Search in sources :

Example 6 with AggregateObjectMapping

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

the class ReferenceDescriptorIsNotAggregateTest method buildEmployeeDescriptor.

public RelationalDescriptor buildEmployeeDescriptor() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
    descriptor.addTableName("EMPLOYEE");
    descriptor.addTableName("SALARY");
    descriptor.addPrimaryKeyFieldName("EMPLOYEE.EMP_ID");
    // Interface properties.
    // descriptor.getInterfacePolicy().addParentInterface(org.eclipse.persistence.testing.models.employee.interfaces.Employee.class);
    // Descriptor properties.
    descriptor.useFullIdentityMap();
    descriptor.setIdentityMapSize(100);
    descriptor.useRemoteFullIdentityMap();
    descriptor.setRemoteIdentityMapSize(100);
    descriptor.setSequenceNumberFieldName("EMP_ID");
    descriptor.setSequenceNumberName("EMP_SEQ");
    // Query manager.
    descriptor.getQueryManager().checkCacheForDoesExist();
    // Event manager.
    // Mappings.
    DirectToFieldMapping idMapping = new DirectToFieldMapping();
    idMapping.setAttributeName("id");
    idMapping.setFieldName("EMPLOYEE.EMP_ID");
    descriptor.addMapping(idMapping);
    AggregateObjectMapping periodMapping = new AggregateObjectMapping();
    periodMapping.setAttributeName("period");
    // periodMapping.setReferenceClass(org.eclipse.persistence.testing.models.employee.domain.EmploymentPeriod.class);
    periodMapping.setReferenceClass(org.eclipse.persistence.testing.models.employee.domain.Employee.class);
    // works    periodMapping.setReferenceClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
    periodMapping.setIsNullAllowed(true);
    descriptor.addMapping(periodMapping);
    return descriptor;
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping)

Example 7 with AggregateObjectMapping

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

the class SQLSelectStatement method fieldsContainField.

/**
 * Check if the field from the field expression is already contained in the select clause of the statement.
 * This is used on order by expression when the field being ordered by must be in the select,
 * but cannot be in the select twice.
 */
protected boolean fieldsContainField(List fields, Expression expression) {
    DatabaseField orderByField;
    if (expression instanceof DataExpression) {
        orderByField = ((DataExpression) expression).getField();
    } else {
        return false;
    }
    // check all fields for a match
    for (Object fieldOrExpression : fields) {
        if (fieldOrExpression instanceof DatabaseField) {
            DatabaseField field = (DatabaseField) fieldOrExpression;
            DataExpression exp = (DataExpression) expression;
            if (field.equals(orderByField)) {
                // Ignore aggregates
                while (((DataExpression) exp.getBaseExpression()).getMapping() instanceof AggregateObjectMapping) {
                    exp = (DataExpression) exp.getBaseExpression();
                }
                if (exp.getBaseExpression() == getBuilder()) {
                    // found a match
                    return true;
                }
            }
        } else // exception.
        if (fieldOrExpression != null) {
            Expression exp = (Expression) fieldOrExpression;
            DatabaseTable table = orderByField.getTable();
            if (exp.getFields().contains(orderByField) && (expression.aliasForTable(table).equals(exp.aliasForTable(table)))) {
                // found a match
                return true;
            }
        }
    }
    // no matches
    return false;
}
Also used : AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DecoratedDatabaseTable(org.eclipse.persistence.internal.history.DecoratedDatabaseTable) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable)

Example 8 with AggregateObjectMapping

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

the class NoSubClassMatchTest_AggregateObject method setup.

@Override
protected void setup() {
    expectedException = DescriptorException.noSubClassMatch(null, new AggregateObjectMapping());
    Employee employee = new Employee();
    ClassDescriptor descriptor = getSession().getDescriptor(org.eclipse.persistence.testing.models.employee.domain.Employee.class);
    ClassDescriptor projDescriptor = getSession().getDescriptor(org.eclipse.persistence.testing.models.employee.domain.SmallProject.class);
    mapping = (org.eclipse.persistence.mappings.AggregateObjectMapping) descriptor.getMappingForAttributeName("period");
    orgInheritancePolicy = mapping.getReferenceDescriptor().getInheritancePolicyOrNull();
    org.eclipse.persistence.internal.sessions.ObjectChangeSet changeSet = new ObjectChangeSet(new Vector(), projDescriptor, employee, new org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet(), true);
    changeRecord = new org.eclipse.persistence.internal.sessions.AggregateChangeRecord(changeSet);
    changeRecord.setChangedObject(changeSet);
    orgIntegrityChecker = getSession().getIntegrityChecker();
    getSession().setIntegrityChecker(new IntegrityChecker());
    getSession().getIntegrityChecker().dontCatchExceptions();
}
Also used : IntegrityChecker(org.eclipse.persistence.exceptions.IntegrityChecker) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) Vector(java.util.Vector)

Example 9 with AggregateObjectMapping

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

the class ReportQueryResult method processItem.

/**
 * INTERNAL:
 * Return a value from an item and database row (converted from raw field values using the mapping).
 */
protected Object processItem(ReportQuery query, AbstractRecord row, Vector toManyData, ReportItem item) {
    JoinedAttributeManager joinManager = null;
    if (item.hasJoining()) {
        joinManager = item.getJoinedAttributeManager();
        if (joinManager.isToManyJoin()) {
            // PERF: Only reset data-result if unset, must only occur once per item, not per row (n vs n^2).
            if (joinManager.getDataResults_() == null) {
                joinManager.setDataResults(new ArrayList(toManyData), query.getSession());
            }
        }
    }
    Object value = null;
    int rowSize = row.size();
    int itemIndex = item.getResultIndex();
    DatabaseMapping mapping = item.getMapping();
    ClassDescriptor descriptor = item.getDescriptor();
    if (item.getAttributeExpression() != null) {
        if (descriptor == null && mapping != null) {
            descriptor = mapping.getReferenceDescriptor();
        }
        if (mapping != null && (mapping.isAbstractColumnMapping() || mapping.isDirectCollectionMapping())) {
            if (itemIndex >= rowSize) {
                throw QueryException.reportQueryResultSizeMismatch(itemIndex + 1, rowSize);
            }
            value = processItemFromMapping(query, row, mapping, item, itemIndex);
            // GF_ISSUE_395+
            if (this.key != null) {
                this.key.append(value);
                this.key.append("_");
            }
        } else if (descriptor != null) {
            // Item is for an object result.
            int size = descriptor.getAllSelectionFields(query).size();
            if (itemIndex + size > rowSize) {
                throw QueryException.reportQueryResultSizeMismatch(itemIndex + size, rowSize);
            }
            AbstractRecord subRow = row;
            // Check if at the start of the row, then avoid building a subRow.
            if (itemIndex > 0) {
                Vector<DatabaseField> trimedFields = new NonSynchronizedSubVector<>(row.getFields(), itemIndex, rowSize);
                Vector trimedValues = new NonSynchronizedSubVector(row.getValues(), itemIndex, rowSize);
                subRow = new DatabaseRecord(trimedFields, trimedValues);
            }
            if (mapping != null && mapping.isAggregateObjectMapping()) {
                value = ((AggregateObjectMapping) mapping).buildAggregateFromRow(subRow, null, null, joinManager, query, false, query.getSession(), true);
            } else {
                // TODO : Support prefrechedCacheKeys in report query
                value = descriptor.getObjectBuilder().buildObject(query, subRow, joinManager);
            }
            // a specific mapping.  This could happen in a MapContainerPolicy
            if (item.getAttributeExpression().isMapEntryExpression() && mapping.isCollectionMapping()) {
                Object rowKey = null;
                if (mapping.getContainerPolicy().isMapPolicy() && !mapping.getContainerPolicy().isMappedKeyMapPolicy()) {
                    rowKey = mapping.getContainerPolicy().keyFrom(value, query.getSession());
                } else {
                    rowKey = mapping.getContainerPolicy().buildKey(subRow, query, null, query.getSession(), true);
                }
                if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
                    value = new Association(rowKey, value);
                } else {
                    value = rowKey;
                }
            }
            // GF_ISSUE_395
            if (this.key != null) {
                Object primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromRow(subRow, query.getSession());
                if (primaryKey != null) {
                    // GF3233 NPE is caused by processing the null PK being extracted from referenced target with null values in database.
                    this.key.append(primaryKey);
                }
                this.key.append("_");
            }
        } else {
            value = row.getValues().get(itemIndex);
            // GF_ISSUE_395
            if (this.key != null) {
                this.key.append(value);
            }
        }
    }
    return value;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) JoinedAttributeManager(org.eclipse.persistence.internal.queries.JoinedAttributeManager) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Association(org.eclipse.persistence.mappings.Association) NonSynchronizedSubVector(org.eclipse.persistence.internal.helper.NonSynchronizedSubVector) Vector(java.util.Vector) NonSynchronizedSubVector(org.eclipse.persistence.internal.helper.NonSynchronizedSubVector)

Example 10 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 = helper.getSession().getClassDescriptorForAlias("SimpleA");
    assertNotNull("No descriptor found for alias='SimpleA'", descriptorA);
    DynamicTypeImpl simpleTypeA = (DynamicTypeImpl) helper.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 = helper.getSession().getClassDescriptorForAlias("SimpleB");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicTypeImpl simpleTypeB = (DynamicTypeImpl) helper.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 = helper.getSession().getClassDescriptorForAlias("SimpleC");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicTypeImpl simpleTypeC = (DynamicTypeImpl) helper.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) DynamicTypeImpl(org.eclipse.persistence.internal.dynamic.DynamicTypeImpl) Test(org.junit.Test)

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