Search in sources :

Example 11 with AggregateObjectMapping

use of org.eclipse.persistence.mappings.AggregateObjectMapping in project cuba by cuba-platform.

the class EmbeddedAttributesMappingProcessor method process.

@Override
public void process(MappingProcessorContext context) {
    DatabaseMapping mapping = context.getMapping();
    if (mapping instanceof AggregateObjectMapping) {
        ClassDescriptor descriptor = mapping.getDescriptor();
        Field referenceField = FieldUtils.getFieldsListWithAnnotation(descriptor.getJavaClass(), EmbeddedParameters.class).stream().filter(f -> f.getName().equals(mapping.getAttributeName())).findFirst().orElse(null);
        if (referenceField != null) {
            EmbeddedParameters embeddedParameters = referenceField.getAnnotation(EmbeddedParameters.class);
            if (!embeddedParameters.nullAllowed())
                ((AggregateObjectMapping) mapping).setIsNullAllowed(false);
        }
    }
}
Also used : EmbeddedParameters(com.haulmont.cuba.core.entity.annotation.EmbeddedParameters) Field(java.lang.reflect.Field) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 12 with AggregateObjectMapping

use of org.eclipse.persistence.mappings.AggregateObjectMapping in project eclipselink by eclipse-ee4j.

the class SimpleTypes_AggregateObject method verifyConfig.

@Test
public void verifyConfig() throws Exception {
    ClassDescriptor descriptorA = dynamicHelper.getSession().getClassDescriptorForAlias("SimpleA");
    assertNotNull("No descriptor found for alias='SimpleA'", descriptorA);
    DynamicType simpleTypeA = dynamicHelper.getType("SimpleA");
    assertNotNull("'SimpleA' EntityType not found", simpleTypeA);
    assertEquals(descriptorA, simpleTypeA.getDescriptor());
    DirectToFieldMapping a_id = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("id");
    assertEquals(int.class, a_id.getAttributeClassification());
    DirectToFieldMapping a_value1 = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("value1");
    assertEquals(String.class, a_value1.getAttributeClassification());
    ClassDescriptor descriptorB = dynamicHelper.getSession().getClassDescriptorForAlias("SimpleB");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicType simpleTypeB = dynamicHelper.getType("SimpleB");
    assertNotNull("'SimpleB' EntityType not found", simpleTypeB);
    assertEquals(descriptorB, simpleTypeB.getDescriptor());
    DirectToFieldMapping b_value2 = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("value2");
    assertEquals(boolean.class, b_value2.getAttributeClassification());
    DirectToFieldMapping b_value3 = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("value3");
    assertEquals(String.class, b_value3.getAttributeClassification());
    assertTrue(descriptorB.isAggregateDescriptor());
    AggregateObjectMapping a_b = (AggregateObjectMapping) descriptorA.getMappingForAttributeName("b");
    assertSame(descriptorB.getJavaClass(), a_b.getReferenceDescriptor().getJavaClass());
    assertTrue(a_b.isNullAllowed());
    ClassDescriptor descriptorC = dynamicHelper.getSession().getClassDescriptorForAlias("SimpleC");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicType simpleTypeC = dynamicHelper.getType("SimpleC");
    assertNotNull("'SimpleC' EntityType not found", simpleTypeC);
    assertEquals(descriptorB, simpleTypeB.getDescriptor());
    DirectToFieldMapping c_value4 = (DirectToFieldMapping) descriptorC.getMappingForAttributeName("value4");
    assertEquals(double.class, c_value4.getAttributeClassification());
    DirectToFieldMapping c_value5 = (DirectToFieldMapping) descriptorC.getMappingForAttributeName("value5");
    assertEquals(String.class, c_value5.getAttributeClassification());
    assertTrue(descriptorB.isAggregateDescriptor());
    AggregateObjectMapping a_c = (AggregateObjectMapping) descriptorA.getMappingForAttributeName("c");
    assertSame(descriptorC.getJavaClass(), a_c.getReferenceDescriptor().getJavaClass());
    assertFalse(a_c.isNullAllowed());
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Example 13 with AggregateObjectMapping

use of org.eclipse.persistence.mappings.AggregateObjectMapping in project eclipselink by eclipse-ee4j.

the class DynamicTypeBuilder method addAggregateObjectMapping.

/**
 * Add a {@link AggregateObjectMapping} ({@link Embeddable} in JPA) to the
 * {@link #entityType} being built or extended.
 *
 * @param name
 *            attribute name to use in the dynamic entity. Also the property
 *            name used to access the state of the entity
 * @param refType
 *            dynamic type re[presenting the Embeddable/AggregateObject
 * @param allowsNull
 *            true indicates that the attribute can be null if all values
 *            are null.
 * @return the new mapping configured and initialized (if the descriptor has
 *         been initialized).
 */
public AggregateObjectMapping addAggregateObjectMapping(String name, DynamicType refType, boolean allowsNull) {
    AggregateObjectMapping mapping = new AggregateObjectMapping();
    mapping.setAttributeName(name);
    mapping.setReferenceClass(refType.getJavaClass());
    mapping.setIsNullAllowed(allowsNull);
    return (AggregateObjectMapping) addMapping(mapping);
}
Also used : AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping)

Example 14 with AggregateObjectMapping

use of org.eclipse.persistence.mappings.AggregateObjectMapping in project eclipselink by eclipse-ee4j.

the class ClassDescriptor method postInitialize.

/**
 * INTERNAL:
 * Post initializations after mappings are initialized.
 */
public void postInitialize(AbstractSession session) throws DescriptorException {
    // These cached settings on the project must be set even if descriptor is initialized.
    if (getHistoryPolicy() != null) {
        session.getProject().setHasGenericHistorySupport(true);
    }
    // Avoid repetitive initialization (this does not solve loops)
    if (isInitialized(POST_INITIALIZED) || isInvalid()) {
        return;
    }
    setInitializationStage(POST_INITIALIZED);
    // this initialize bottom up, unlike the two other phases that to top down.
    if (hasInheritance()) {
        for (ClassDescriptor child : getInheritancePolicy().getChildDescriptors()) {
            child.postInitialize(session);
        }
    }
    // Allow mapping to perform post initialization.
    for (DatabaseMapping mapping : getMappings()) {
        // This causes post init to be called multiple times in inheritance.
        mapping.postInitialize(session);
        // PERF: computed if deferred locking is required.
        if (!shouldAcquireCascadedLocks()) {
            if (mapping.isForeignReferenceMapping()) {
                if (!((ForeignReferenceMapping) mapping).usesIndirection()) {
                    setShouldAcquireCascadedLocks(true);
                }
                hasRelationships = true;
            }
            if ((mapping instanceof AggregateObjectMapping)) {
                if (mapping.getReferenceDescriptor().shouldAcquireCascadedLocks()) {
                    setShouldAcquireCascadedLocks(true);
                }
                if (mapping.getReferenceDescriptor().hasRelationships()) {
                    hasRelationships = true;
                }
            }
        }
        if (getCachePolicy().isProtectedIsolation() && ((mapping.isForeignReferenceMapping() && !mapping.isCacheable()) || (mapping.isAggregateObjectMapping() && mapping.getReferenceDescriptor().hasNoncacheableMappings()))) {
            mapping.collectQueryParameters(this.foreignKeyValuesForCaching);
        }
        if (mapping.isLockableMapping()) {
            getLockableMappings().add(mapping);
        }
    }
    if (hasInheritance()) {
        getInheritancePolicy().postInitialize(session);
    }
    // PERF: Ensure that the identical primary key fields are used to avoid equals.
    for (int index = (getPrimaryKeyFields().size() - 1); index >= 0; index--) {
        DatabaseField primaryKeyField = getPrimaryKeyFields().get(index);
        int fieldIndex = getFields().indexOf(primaryKeyField);
        // Aggregate/agg-collections may not have a mapping for pk field.
        if (fieldIndex != -1) {
            primaryKeyField = getFields().get(fieldIndex);
            getPrimaryKeyFields().set(index, primaryKeyField);
            primaryKeyField.setPrimaryKey(true);
        }
    }
    // List of fields selected by a query that uses SOP when descriptor has SOP. Used to index these fields.
    List<DatabaseField> sopSelectionFields = null;
    if (hasSerializedObjectPolicy()) {
        getSerializedObjectPolicy().postInitialize(session);
        this.selectionFields = (List<DatabaseField>) getFields().clone();
        this.selectionFields.remove(getSerializedObjectPolicy().getField());
        this.allSelectionFields = (List<DatabaseField>) getAllFields().clone();
        this.allSelectionFields.remove(getSerializedObjectPolicy().getField());
        sopSelectionFields = getSerializedObjectPolicy().getSelectionFields();
        if (sopSelectionFields.size() == getFields().size()) {
            // no need for sop field indexes - SOP uses all the field in the descriptor
            sopSelectionFields = null;
        }
    } else {
        this.selectionFields = getFields();
        this.allSelectionFields = getAllFields();
    }
    // May 02, 2000 - Jon D.
    for (int index = 0; index < getFields().size(); index++) {
        DatabaseField field = getFields().elementAt(index);
        if (field.getType() == null) {
            DatabaseMapping mapping = getObjectBuilder().getMappingForField(field);
            if (mapping != null) {
                field.setType(mapping.getFieldClassification(field));
            }
        }
        // LOB may require lob writer which needs full row to fetch LOB.
        if ((field.getType() == ClassConstants.BLOB) || (field.getType() == ClassConstants.CLOB)) {
            setHasMultipleTableConstraintDependecy(true);
        }
        field.setIndex(index);
        if (sopSelectionFields != null) {
            int sopFieldIndex = sopSelectionFields.indexOf(field);
            if (sopFieldIndex != -1) {
                field.setIndex(sopFieldIndex);
            }
        }
    }
    // initialized fields, post-initialize any source and target Expression fields.
    for (DatabaseMapping mapping : getMappings()) {
        mapping.postInitializeSourceAndTargetExpressions();
    }
    // Set cache key type.
    if (getCachePolicy().getCacheKeyType() == null || (getCachePolicy().getCacheKeyType() == CacheKeyType.AUTO)) {
        if ((getPrimaryKeyFields().size() > 1) || getObjectBuilder().isXMLObjectBuilder()) {
            setCacheKeyType(CacheKeyType.CACHE_ID);
        } else if ((getPrimaryKeyFields().size() == 1) && (getObjectBuilder().getPrimaryKeyClassifications().size() == 1)) {
            Class<?> type = getObjectBuilder().getPrimaryKeyClassifications().get(0);
            if ((type == null) || type.isArray()) {
                getCachePolicy().setCacheKeyType(CacheKeyType.CACHE_ID);
            } else {
                getCachePolicy().setCacheKeyType(CacheKeyType.ID_VALUE);
            }
        } else {
            getCachePolicy().setCacheKeyType(CacheKeyType.CACHE_ID);
        }
    } else if ((getCachePolicy().getCacheKeyType() == CacheKeyType.ID_VALUE) && (getPrimaryKeyFields().size() > 1)) {
        session.getIntegrityChecker().handleError(DescriptorException.cannotUseIdValueForCompositeId(this));
    }
    if (hasFetchGroupManager()) {
        getFetchGroupManager().postInitialize(session);
    }
    getObjectBuilder().postInitialize(session);
    getQueryManager().postInitialize(session);
    // Post initialize the multitenant policy after the query manager.
    if (hasMultitenantPolicy()) {
        getMultitenantPolicy().postInitialize(session);
    }
    getCachePolicy().postInitialize(this, session);
    postInitializeReturningPolicies();
    validateAfterInitialization(session);
    checkDatabase(session);
}
Also used : AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 15 with AggregateObjectMapping

use of org.eclipse.persistence.mappings.AggregateObjectMapping in project eclipselink by eclipse-ee4j.

the class IsolatedEmployeeProject method buildEmployeeDescriptor.

public RelationalDescriptor buildEmployeeDescriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedEmployee.class);
    descriptor.addTableName("ISOLATED_EMPLOYEE");
    descriptor.addTableName("ISOLATED_SALARY");
    descriptor.addPrimaryKeyFieldName("ISOLATED_EMPLOYEE.EMP_ID");
    // Descriptor properties.
    descriptor.setSequenceNumberFieldName("EMP_ID");
    descriptor.setSequenceNumberName("EMP_SEQ");
    VersionLockingPolicy lockingPolicy = new VersionLockingPolicy();
    lockingPolicy.setWriteLockFieldName("VERSION");
    descriptor.setOptimisticLockingPolicy(lockingPolicy);
    descriptor.setCacheIsolation(CacheIsolationType.ISOLATED);
    // Tests depend on uow merge.
    descriptor.setUnitOfWorkCacheIsolationLevel(RelationalDescriptor.ISOLATE_NEW_DATA_AFTER_TRANSACTION);
    // Query manager.
    descriptor.getQueryManager().checkCacheForDoesExist();
    // Event manager.
    // Mappings.
    DirectToFieldMapping firstNameMapping = new DirectToFieldMapping();
    firstNameMapping.setAttributeName("firstName");
    firstNameMapping.setFieldName("ISOLATED_EMPLOYEE.F_NAME");
    firstNameMapping.setNullValue("");
    descriptor.addMapping(firstNameMapping);
    DirectToFieldMapping idMapping = new DirectToFieldMapping();
    idMapping.setAttributeName("id");
    idMapping.setFieldName("ISOLATED_EMPLOYEE.EMP_ID");
    descriptor.addMapping(idMapping);
    DirectToFieldMapping lastNameMapping = new DirectToFieldMapping();
    lastNameMapping.setAttributeName("lastName");
    lastNameMapping.setFieldName("ISOLATED_EMPLOYEE.L_NAME");
    lastNameMapping.setNullValue("");
    descriptor.addMapping(lastNameMapping);
    DirectToFieldMapping salaryMapping = new DirectToFieldMapping();
    salaryMapping.setAttributeName("salary");
    salaryMapping.setFieldName("ISOLATED_SALARY.SALARY");
    descriptor.addMapping(salaryMapping);
    DirectToFieldMapping genderMapping = new DirectToFieldMapping();
    ObjectTypeConverter genderConverter = new ObjectTypeConverter();
    genderMapping.setAttributeName("gender");
    genderMapping.setFieldName("ISOLATED_EMPLOYEE.GENDER");
    genderConverter.addConversionValue("M", "Male");
    genderConverter.addConversionValue("F", "Female");
    genderMapping.setConverter(genderConverter);
    descriptor.addMapping(genderMapping);
    TransformationMapping normalHoursMapping = new TransformationMapping();
    normalHoursMapping.setAttributeName("normalHours");
    normalHoursMapping.setAttributeTransformation("buildNormalHours");
    normalHoursMapping.addFieldTransformation("ISOLATED_EMPLOYEE.START_TIME", "getStartTime");
    normalHoursMapping.addFieldTransformation("ISOLATED_EMPLOYEE.END_TIME", "getEndTime");
    descriptor.addMapping(normalHoursMapping);
    AggregateObjectMapping periodMapping = new AggregateObjectMapping();
    periodMapping.setAttributeName("period");
    periodMapping.setReferenceClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedEmploymentPeriod.class);
    periodMapping.setIsNullAllowed(true);
    descriptor.addMapping(periodMapping);
    DirectCollectionMapping responsibilitiesListMapping = new DirectCollectionMapping();
    responsibilitiesListMapping.setAttributeName("responsibilitiesList");
    responsibilitiesListMapping.useBasicIndirection();
    responsibilitiesListMapping.setReferenceTableName("ISOLATED_RESPONS");
    responsibilitiesListMapping.setDirectFieldName("ISOLATED_RESPONS.DESCRIP");
    responsibilitiesListMapping.addReferenceKeyFieldName("ISOLATED_RESPONS.EMP_ID", "ISOLATED_EMPLOYEE.EMP_ID");
    descriptor.addMapping(responsibilitiesListMapping);
    OneToOneMapping addressMapping = new OneToOneMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setReferenceClass(org.eclipse.persistence.testing.tests.isolatedsession.IsolatedAddress.class);
    addressMapping.useBasicIndirection();
    addressMapping.privateOwnedRelationship();
    addressMapping.addForeignKeyFieldName("ISOLATED_EMPLOYEE.ADDR_ID", "ISOLATED_ADDRESS.ADDRESS_ID");
    descriptor.addMapping(addressMapping);
    OneToOneMapping managerMapping = new OneToOneMapping();
    managerMapping.setAttributeName("manager");
    managerMapping.setReferenceClass(IsolatedEmployee.class);
    managerMapping.useBasicIndirection();
    managerMapping.addForeignKeyFieldName("ISOLATED_EMPLOYEE.MANAGER_ID", "ISOLATED_EMPLOYEE.EMP_ID");
    descriptor.addMapping(managerMapping);
    OneToManyMapping managedEmployeesMapping = new OneToManyMapping();
    managedEmployeesMapping.setAttributeName("managedEmployees");
    managedEmployeesMapping.setReferenceClass(IsolatedEmployee.class);
    managedEmployeesMapping.useBasicIndirection();
    managedEmployeesMapping.addTargetForeignKeyFieldName("ISOLATED_EMPLOYEE.MANAGER_ID", "ISOLATED_EMPLOYEE.EMP_ID");
    descriptor.addMapping(managedEmployeesMapping);
    OneToManyMapping phoneNumbersMapping = new OneToManyMapping();
    phoneNumbersMapping.setAttributeName("phoneNumbers");
    phoneNumbersMapping.setReferenceClass(IsolatedPhoneNumber.class);
    phoneNumbersMapping.useBasicIndirection();
    phoneNumbersMapping.privateOwnedRelationship();
    phoneNumbersMapping.addTargetForeignKeyFieldName("ISOLATED_PHONE.EMP_ID", "ISOLATED_EMPLOYEE.EMP_ID");
    descriptor.addMapping(phoneNumbersMapping);
    return descriptor;
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) TransformationMapping(org.eclipse.persistence.mappings.TransformationMapping) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) VersionLockingPolicy(org.eclipse.persistence.descriptors.VersionLockingPolicy) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping)

Aggregations

AggregateObjectMapping (org.eclipse.persistence.mappings.AggregateObjectMapping)21 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)10 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)8 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 TransparentIndirectionPolicy (org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy)5 MappedKeyMapContainerPolicy (org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)5 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 ArrayList (java.util.ArrayList)3 Vector (java.util.Vector)3 VersionLockingPolicy (org.eclipse.persistence.descriptors.VersionLockingPolicy)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 Association (org.eclipse.persistence.mappings.Association)2 OneToManyMapping (org.eclipse.persistence.mappings.OneToManyMapping)2 UnidirectionalOneToManyMapping (org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping)2 ObjectTypeConverter (org.eclipse.persistence.mappings.converters.ObjectTypeConverter)2 Test (org.junit.Test)2 EmbeddedParameters (com.haulmont.cuba.core.entity.annotation.EmbeddedParameters)1 EmbeddedParameters (io.jmix.core.entity.annotation.EmbeddedParameters)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1