/** INTERNAL: Print SQL onto the stream, using the ExpressionPrinter for context */
  public void printSQL(ExpressionSQLPrinter printer) {
    if (isAttribute()) {
      printer.printField(getAliasedField());
    }

    // If the mapping is a direct collection then this falls into a gray area.
    // It must be treated as an attribute at this moment for it has a direct field.
    // However it is not an attribute in the sense that it also represents a foreign
    // reference and a mapping criteria has been added.
    // For bug 2900974 these are now handled as non-attributes during normalize but
    // as attributes when printing SQL.
    //
    if ((!isAttribute()) && (getMapping() != null) && getMapping().isDirectCollectionMapping()) {
      DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping) getMapping();

      // The aliased table comes for free as it was a required part of the join criteria.
      TableExpression table =
          (TableExpression) getTable(directCollectionMapping.getReferenceTable());
      DatabaseTable aliasedTable = table.aliasForTable(table.getTable());
      DatabaseField aliasedField = (DatabaseField) directCollectionMapping.getDirectField().clone();
      aliasedField.setTable(aliasedTable);
      printer.printField(aliasedField);
    }

    if ((getMapping() != null) && getMapping().isNestedTableMapping()) {
      DatabaseTable tableAlias = aliasForTable(new NestedTable(this));
      printer.printString(tableAlias.getName());
    }
  }
 public void reset() {
   UnitOfWork uow = getSession().acquireUnitOfWork();
   uow.deleteAllObjects(holders);
   uow.commit();
   if (!verifyDelete(holders.get(0))) {
     throw new TestErrorException("Delete was unsuccessful.");
   }
   mapping.setJoinFetch(oldFetchJoinValue);
 }
  public void setup() {
    mapping =
        (DirectCollectionMapping)
            getSession()
                .getProject()
                .getDescriptor(DirectDirectMapHolder.class)
                .getMappingForAttributeName("directToDirectMap");
    oldFetchJoinValue = mapping.getJoinFetch();
    mapping.setJoinFetch(fetchJoinRelationship);
    getSession()
        .getProject()
        .getDescriptor(DirectDirectMapHolder.class)
        .reInitializeJoinedAttributes();

    UnitOfWork uow = getSession().acquireUnitOfWork();
    DirectDirectMapHolder holder = new DirectDirectMapHolder();
    holder.addDirectToDirectMapItem(new Integer(1), new Integer(1));
    holder.addDirectToDirectMapItem(new Integer(2), new Integer(2));
    uow.registerObject(holder);
    uow.commit();
    holderExp = (new ExpressionBuilder()).get("id").equal(holder.getId());
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
  }
Esempio n. 4
0
  public static RelationalDescriptor descriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();

    /* First define the class, table and descriptor properties. */
    descriptor.setJavaClass(Customer.class);
    descriptor.setTableName("EVENTCUSTOMER");
    descriptor.setPrimaryKeyFieldName("ID");
    descriptor.setSequenceNumberName("SEQ");
    descriptor.setSequenceNumberFieldName("ID");

    /* Next define the attribute mappings. */
    OneToOneMapping addressMapping = new OneToOneMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setReferenceClass(Address.class);
    addressMapping.dontUseIndirection();
    addressMapping.privateOwnedRelationship();
    addressMapping.addForeignKeyFieldName("EVENTCUSTOMER.ADDRESS_ID", "EADDRESS.ID");
    descriptor.addMapping(addressMapping);

    OneToOneMapping phoneMapping = new OneToOneMapping();
    phoneMapping.setAttributeName("phoneNumber");
    phoneMapping.setReferenceClass(Phone.class);
    phoneMapping.dontUseIndirection();
    phoneMapping.privateOwnedRelationship();
    phoneMapping.addForeignKeyFieldName("EVENTCUSTOMER.PHONE_ID", "EPHONE.ID");
    descriptor.addMapping(phoneMapping);

    OneToOneMapping emailMapping = new OneToOneMapping();
    emailMapping.setAttributeName("email");
    emailMapping.setReferenceClass(EmailAccount.class);
    emailMapping.dontUseIndirection();
    emailMapping.privateOwnedRelationship();
    emailMapping.addForeignKeyFieldName("EVENTCUSTOMER.EMAIL_ID", "EMAILACC.ID");
    descriptor.addMapping(emailMapping);

    DirectCollectionMapping associationsMapping = new DirectCollectionMapping();
    associationsMapping.setAttributeName("associations");
    associationsMapping.dontUseIndirection();
    associationsMapping.setReferenceTableName("EASSOCIATIONS");
    associationsMapping.setDirectFieldName("EASSOCIATIONS.DESCRIP");
    associationsMapping.addReferenceKeyFieldName("EASSOCIATIONS.CUSTOMER_ID", "EVENTCUSTOMER.ID");
    descriptor.addMapping(associationsMapping);

    OneToManyMapping ordersMapping = new OneToManyMapping();
    ordersMapping.setAttributeName("orders");
    ordersMapping.setReferenceClass(Order.class);
    ordersMapping.useBasicIndirection();
    ordersMapping.addTargetForeignKeyFieldName("EVENTORDER.CUSTOMER_ID", "EVENTCUSTOMER.ID");
    descriptor.addMapping(ordersMapping);

    AggregateObjectMapping creditMapping = new AggregateObjectMapping();
    creditMapping.setAttributeName("creditCard");
    creditMapping.setReferenceClass(org.eclipse.persistence.testing.models.events.CreditCard.class);
    creditMapping.setIsNullAllowed(true);
    descriptor.addMapping(creditMapping);

    descriptor.addDirectMapping("id", "ID");
    descriptor.addDirectMapping("name", "NAME");

    return descriptor;
  }
  public ClassDescriptor buildEmployeeDescriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(Employee.class);
    descriptor.addTableName("OTOJT_EMPLOYEE");
    descriptor.addTableName("OTOJT_SALARY");
    descriptor.addPrimaryKeyFieldName("OTOJT_EMPLOYEE.EMP_ID");

    // Descriptor Properties.
    descriptor.useSoftCacheWeakIdentityMap();
    descriptor.setIdentityMapSize(100);
    descriptor.setSequenceNumberFieldName("OTOJT_EMPLOYEE.EMP_ID");
    descriptor.setSequenceNumberName("OTOJT_EMP_SEQ");
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("OTOJT_EMPLOYEE.VERSION");
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    descriptor.setAlias("OTOJT_Employee");

    // Cache Invalidation Policy
    // Query Manager.
    descriptor.getQueryManager().checkCacheForDoesExist();

    // Named Queries.
    // Event Manager.
    // Mappings.
    DirectToFieldMapping firstNameMapping = new DirectToFieldMapping();
    firstNameMapping.setAttributeName("firstName");
    firstNameMapping.setFieldName("OTOJT_EMPLOYEE.F_NAME");
    firstNameMapping.setNullValue("");
    descriptor.addMapping(firstNameMapping);

    DirectToFieldMapping idMapping = new DirectToFieldMapping();
    idMapping.setAttributeName("id");
    idMapping.setFieldName("OTOJT_EMPLOYEE.EMP_ID");
    descriptor.addMapping(idMapping);

    DirectToFieldMapping lastNameMapping = new DirectToFieldMapping();
    lastNameMapping.setAttributeName("lastName");
    lastNameMapping.setFieldName("OTOJT_EMPLOYEE.L_NAME");
    lastNameMapping.setNullValue("");
    descriptor.addMapping(lastNameMapping);

    DirectToFieldMapping salaryMapping = new DirectToFieldMapping();
    salaryMapping.setAttributeName("salary");
    salaryMapping.setFieldName("OTOJT_SALARY.SALARY");
    descriptor.addMapping(salaryMapping);

    DirectToFieldMapping genderMapping = new DirectToFieldMapping();
    genderMapping.setAttributeName("gender");
    genderMapping.setFieldName("OTOJT_EMPLOYEE.GENDER");
    ObjectTypeConverter genderMappingConverter = new ObjectTypeConverter();
    genderMappingConverter.addConversionValue("F", "Female");
    genderMappingConverter.addConversionValue("M", "Male");
    genderMapping.setConverter(genderMappingConverter);
    descriptor.addMapping(genderMapping);

    DirectCollectionMapping responsibilitiesListMapping = new DirectCollectionMapping();
    responsibilitiesListMapping.setAttributeName("responsibilitiesList");
    responsibilitiesListMapping.useTransparentList();
    responsibilitiesListMapping.setReferenceTableName("OTOJT_RESPONS");
    responsibilitiesListMapping.setDirectFieldName("OTOJT_RESPONS.DESCRIP");
    responsibilitiesListMapping.addReferenceKeyFieldName(
        "OTOJT_RESPONS.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    descriptor.addMapping(responsibilitiesListMapping);

    OneToOneMapping addressMapping = new OneToOneMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setReferenceClass(Address.class);
    addressMapping.useBasicIndirection();
    addressMapping.privateOwnedRelationship();
    //        addressMapping.addForeignKeyFieldName("OTOJT_EMPLOYEE.ADDR_ID",
    // "OTOJT_ADDRESS.ADDRESS_ID");
    addressMapping.setRelationTableMechanism(new RelationTableMechanism());
    addressMapping.getRelationTableMechanism().setRelationTableName("OTOJT_EMP_ADDRESS");
    addressMapping
        .getRelationTableMechanism()
        .addSourceRelationKeyFieldName("OTOJT_EMP_ADDRESS.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    addressMapping
        .getRelationTableMechanism()
        .addTargetRelationKeyFieldName("OTOJT_EMP_ADDRESS.ADDR_ID", "OTOJT_ADDRESS.ADDRESS_ID");
    descriptor.addMapping(addressMapping);

    // Joel:EJBQLTesting
    OneToOneMapping managerMapping = new OneToOneMapping();
    managerMapping.setAttributeName("manager");
    managerMapping.setReferenceClass(Employee.class);
    managerMapping.useBasicIndirection();
    //        managerMapping.addForeignKeyFieldName("OTOJT_EMPLOYEE.MANAGER_ID",
    // "OTOJT_EMPLOYEE.EMP_ID");
    managerMapping.setRelationTableMechanism(new RelationTableMechanism());
    managerMapping.getRelationTableMechanism().setRelationTableName("OTOJT_EMP_MANAGER");
    managerMapping
        .getRelationTableMechanism()
        .addSourceRelationKeyFieldName("OTOJT_EMP_MANAGER.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    managerMapping
        .getRelationTableMechanism()
        .addTargetRelationKeyFieldName("OTOJT_EMP_MANAGER.MANAGER_ID", "OTOJT_EMPLOYEE.EMP_ID");
    descriptor.addMapping(managerMapping);

    //        OneToManyMapping managedEmployeesMapping = new OneToManyMapping();
    ManyToManyMapping managedEmployeesMapping = new ManyToManyMapping();
    managedEmployeesMapping.setAttributeName("managedEmployees");
    managedEmployeesMapping.setReferenceClass(Employee.class);
    managedEmployeesMapping.useTransparentList();
    //        managedEmployeesMapping.addTargetForeignKeyFieldName("OTOJT_EMPLOYEE.MANAGER_ID",
    // "OTOJT_EMPLOYEE.EMP_ID");
    managedEmployeesMapping.setRelationTableName("OTOJT_EMP_MANAGER");
    managedEmployeesMapping.addSourceRelationKeyFieldName(
        "OTOJT_EMP_MANAGER.MANAGER_ID", "OTOJT_EMPLOYEE.EMP_ID");
    managedEmployeesMapping.addTargetRelationKeyFieldName(
        "OTOJT_EMP_MANAGER.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    managedEmployeesMapping.readOnly();
    descriptor.addMapping(managedEmployeesMapping);

    //        OneToManyMapping childrenMapping = new OneToManyMapping();
    ManyToManyMapping childrenMapping = new ManyToManyMapping();
    childrenMapping.setAttributeName("children");
    childrenMapping.setReferenceClass(Child.class);
    childrenMapping.addAscendingOrdering("birthday");
    childrenMapping.useTransparentList();
    childrenMapping.privateOwnedRelationship();
    //        childrenMapping.addTargetForeignKeyFieldName("OTOJT_CHILD.PARENT_EMP_ID",
    // "OTOJT_EMPLOYEE.EMP_ID");
    childrenMapping.setRelationTableName("OTOJT_CHILD_PARENT");
    childrenMapping.addSourceRelationKeyFieldName(
        "OTOJT_CHILD_PARENT.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    childrenMapping.addTargetRelationKeyFieldName(
        "OTOJT_CHILD_PARENT.CHILD_ID", "OTOJT_CHILD.CHILD_ID");
    descriptor.addMapping(childrenMapping);

    ManyToManyMapping projectsMapping = new ManyToManyMapping();
    projectsMapping.setAttributeName("projects");
    projectsMapping.setReferenceClass(Project.class);
    projectsMapping.useTransparentList();
    projectsMapping.setRelationTableName("OTOJT_PROJ_EMP");
    projectsMapping.addSourceRelationKeyFieldName("OTOJT_PROJ_EMP.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    projectsMapping.addTargetRelationKeyFieldName(
        "OTOJT_PROJ_EMP.PROJ_ID", "OTOJT_PROJECT.PROJ_ID");
    descriptor.addMapping(projectsMapping);

    OneToOneMapping projectLedMapping = new OneToOneMapping();
    projectLedMapping.setAttributeName("projectLed");
    projectLedMapping.setReferenceClass(Project.class);
    projectLedMapping.useBasicIndirection();
    projectLedMapping.setRelationTableMechanism(new RelationTableMechanism());
    projectLedMapping.getRelationTableMechanism().setRelationTableName("OTOJT_PROJ_LEADER");
    projectLedMapping
        .getRelationTableMechanism()
        .addSourceRelationKeyFieldName("OTOJT_PROJ_LEADER.EMP_ID", "OTOJT_EMPLOYEE.EMP_ID");
    projectLedMapping
        .getRelationTableMechanism()
        .addTargetRelationKeyFieldName("OTOJT_PROJ_LEADER.PROJ_ID", "PROJ_ID");
    projectLedMapping.readOnly();
    descriptor.addMapping(projectLedMapping);

    return descriptor;
  }
 protected void configureContactContainer2(
     org.eclipse.persistence.mappings.DirectCollectionMapping directcollectionmapping) {
   directcollectionmapping.useCollectionClass(java.util.Stack.class);
 }
  /** modifications are marked with "bjv" */
  protected void buildOrderDescriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();

    // SECTION: DESCRIPTOR
    descriptor.setJavaClass(this.orderClass()); // bjv
    Vector vector = new Vector();
    vector.addElement("ORD");
    descriptor.setTableNames(vector);
    descriptor.addPrimaryKeyFieldName("ORD.ID");

    // SECTION: PROPERTIES
    descriptor.setIdentityMapClass(
        org.eclipse.persistence.internal.identitymaps.FullIdentityMap.class);
    descriptor.setSequenceNumberName("order_seq");
    descriptor.setSequenceNumberFieldName("ID");
    descriptor.setExistenceChecking("Check cache");
    descriptor.setIdentityMapSize(100);

    // SECTION: COPY POLICY
    descriptor.createCopyPolicy("constructor");

    // SECTION: INSTANTIATION POLICY
    descriptor.createInstantiationPolicy("constructor");

    // SECTION: DIRECTCOLLECTIONMAPPING
    org.eclipse.persistence.mappings.DirectCollectionMapping directcollectionmapping =
        new org.eclipse.persistence.mappings.DirectCollectionMapping();
    directcollectionmapping.setAttributeName("contacts");
    directcollectionmapping.setIsReadOnly(false);
    directcollectionmapping.setUsesIndirection(false);
    directcollectionmapping.setIsPrivateOwned(true);
    this.configureContactContainer(directcollectionmapping); // bjv
    directcollectionmapping.setDirectFieldName("CONTACT.NAME");
    directcollectionmapping.setReferenceTableName("CONTACT");
    directcollectionmapping.addReferenceKeyFieldName("CONTACT.ORDER_ID", "ORD.ID");
    descriptor.addMapping(directcollectionmapping);

    // SECTION: DIRECTCOLLECTIONMAPPING
    org.eclipse.persistence.mappings.DirectCollectionMapping directcollectionmapping1 =
        new org.eclipse.persistence.mappings.DirectCollectionMapping();
    directcollectionmapping1.setAttributeName("contacts2");
    directcollectionmapping1.setIsReadOnly(false);
    directcollectionmapping1.setUsesIndirection(false);
    directcollectionmapping1.setIsPrivateOwned(true);
    this.configureContactContainer2(directcollectionmapping1); // bjv
    directcollectionmapping1.setDirectFieldName("CONTACT2.NAME");
    directcollectionmapping1.setReferenceTableName("CONTACT2");
    directcollectionmapping1.addReferenceKeyFieldName("CONTACT2.ORDER_ID", "ORD.ID");
    descriptor.addMapping(directcollectionmapping1);

    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping =
        new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping.setAttributeName("customerName");
    directtofieldmapping.setIsReadOnly(false);
    directtofieldmapping.setFieldName("ORD.CUSTNAME");
    descriptor.addMapping(directtofieldmapping);

    // SECTION: DIRECTTOFIELDMAPPING
    org.eclipse.persistence.mappings.DirectToFieldMapping directtofieldmapping1 =
        new org.eclipse.persistence.mappings.DirectToFieldMapping();
    directtofieldmapping1.setAttributeName("id");
    directtofieldmapping1.setIsReadOnly(false);
    directtofieldmapping1.setFieldName("ORD.ID");
    descriptor.addMapping(directtofieldmapping1);

    // SECTION: MANYTOMANYMAPPING
    org.eclipse.persistence.mappings.ManyToManyMapping manytomanymapping =
        new org.eclipse.persistence.mappings.ManyToManyMapping();
    manytomanymapping.setAttributeName("salesReps");
    manytomanymapping.setIsReadOnly(false);
    manytomanymapping.setUsesIndirection(false);
    manytomanymapping.setReferenceClass(salesRepClass());
    manytomanymapping.setIsPrivateOwned(false);
    this.configureSalesRepContainer(manytomanymapping); // bjv
    manytomanymapping.setRelationTableName("ORDREP");
    manytomanymapping.addSourceRelationKeyFieldName("ORDREP.ORDER_ID", "ORD.ID");
    manytomanymapping.addTargetRelationKeyFieldName("ORDREP.SALEREP_ID", "SALEREP.ID");
    descriptor.addMapping(manytomanymapping);

    // SECTION: MANYTOMANYMAPPING
    org.eclipse.persistence.mappings.ManyToManyMapping manytomanymapping1 =
        new org.eclipse.persistence.mappings.ManyToManyMapping();
    manytomanymapping1.setAttributeName("salesReps2");
    manytomanymapping1.setIsReadOnly(false);
    manytomanymapping1.setUsesIndirection(false);
    manytomanymapping1.setReferenceClass(salesRepClass());
    manytomanymapping1.setIsPrivateOwned(false);
    this.configureSalesRepContainer2(manytomanymapping1); // bjv
    manytomanymapping1.setRelationTableName("ORDREP2");
    manytomanymapping1.addSourceRelationKeyFieldName("ORDREP2.ORDER_ID", "ORD.ID");
    manytomanymapping1.addTargetRelationKeyFieldName("ORDREP2.SALEREP_ID", "SALEREP.ID");
    descriptor.addMapping(manytomanymapping1);

    // SECTION: ONETOMANYMAPPING
    org.eclipse.persistence.mappings.OneToManyMapping onetomanymapping =
        new org.eclipse.persistence.mappings.OneToManyMapping();
    onetomanymapping.setAttributeName("lines");
    onetomanymapping.setIsReadOnly(false);
    onetomanymapping.setUsesIndirection(false);
    onetomanymapping.setReferenceClass(orderLineClass());
    onetomanymapping.setIsPrivateOwned(true);
    this.configureLineContainer(onetomanymapping); // bjv
    onetomanymapping.addTargetForeignKeyFieldName("ORDLINE.ORDER_ID", "ORD.ID");
    descriptor.addMapping(onetomanymapping);

    // SECTION: TRANSFORMATIONMAPPING
    org.eclipse.persistence.mappings.TransformationMapping transformationmapping =
        new org.eclipse.persistence.mappings.TransformationMapping();
    transformationmapping.setAttributeName("total");
    transformationmapping.setIsReadOnly(false);
    transformationmapping.setUsesIndirection(true);
    transformationmapping.setAttributeTransformation("getTotalFromRow");
    transformationmapping.addFieldTransformation("ORD.TOTT", "getTotalTens");
    transformationmapping.addFieldTransformation("ORD.TOTO", "getTotalOnes");
    descriptor.addMapping(transformationmapping);

    // SECTION: TRANSFORMATIONMAPPING
    org.eclipse.persistence.mappings.TransformationMapping transformationmapping2 =
        new org.eclipse.persistence.mappings.TransformationMapping();
    transformationmapping2.setAttributeName("total2");
    transformationmapping2.setIsReadOnly(false);
    transformationmapping2.setUsesIndirection(false);
    transformationmapping2.setAttributeTransformation("getTotalFromRow2");
    transformationmapping2.addFieldTransformation("ORD.TOTT2", "getTotalTens2");
    transformationmapping2.addFieldTransformation("ORD.TOTO2", "getTotalOnes2");
    descriptor.addMapping(transformationmapping2);

    this.modifyOrderDescriptor(descriptor); // bjv
    addDescriptor(descriptor);
  }