Search in sources :

Example 6 with AggregateCollectionMapping

use of org.eclipse.persistence.mappings.AggregateCollectionMapping in project blaze-persistence by Blazebit.

the class EclipseLinkJpaProvider method getJoinTable.

@Override
public JoinTable getJoinTable(EntityType<?> ownerType, String attributeName) {
    DatabaseMapping mapping = getAttribute(ownerType, attributeName).getMapping();
    if (mapping instanceof OneToOneMapping) {
        OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
        if (oneToOneMapping.hasRelationTable()) {
            Map<String, String> idColumnMapping = new LinkedHashMap<>();
            Map<String, String> keyMapping = null;
            Map<String, String> keyColumnTypes = null;
            Map<String, String> targetIdColumnMapping = new LinkedHashMap<>();
            return new JoinTable(oneToOneMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping, keyColumnTypes, null, targetIdColumnMapping);
        }
    } else if (mapping instanceof CollectionMapping) {
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        if (collectionMapping instanceof ManyToManyMapping) {
            ManyToManyMapping manyToManyMapping = (ManyToManyMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = manyToManyMapping.getSourceKeyFields();
            Vector<DatabaseField> sourceRelationKeyFields = manyToManyMapping.getSourceRelationKeyFields();
            Vector<DatabaseField> targetKeyFields = manyToManyMapping.getTargetKeyFields();
            Vector<DatabaseField> targetRelationKeyFields = manyToManyMapping.getTargetRelationKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = new LinkedHashMap<>(targetKeyFields.size());
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), sourceRelationKeyFields.get(i).getName());
            }
            for (int i = 0; i < targetKeyFields.size(); i++) {
                targetIdColumnMapping.put(targetKeyFields.get(i).getName(), targetRelationKeyFields.get(i).getName());
            }
            return new JoinTable(manyToManyMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping(manyToManyMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof DirectCollectionMapping) {
            DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = directCollectionMapping.getSourceKeyFields();
            Vector<DatabaseField> referenceKeyFields = directCollectionMapping.getReferenceKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
            }
            return new JoinTable(directCollectionMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof DirectMapMapping) {
            DirectMapMapping directMapMapping = (DirectMapMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = directMapMapping.getSourceKeyFields();
            Vector<DatabaseField> referenceKeyFields = directMapMapping.getReferenceKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
            }
            return new JoinTable(directMapMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directMapMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof AggregateCollectionMapping) {
            AggregateCollectionMapping aggregateCollectionMapping = (AggregateCollectionMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = aggregateCollectionMapping.getSourceKeyFields();
            Vector<DatabaseField> targetForeignKeyFields = aggregateCollectionMapping.getTargetForeignKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            String tableName = null;
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                tableName = targetForeignKeyFields.get(i).getTableName();
                idColumnMapping.put(sourceKeyFields.get(i).getName(), targetForeignKeyFields.get(i).getName());
            }
            return new JoinTable(tableName, null, idColumnMapping, keyMapping(aggregateCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        }
    }
    return null;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) LinkedHashMap(java.util.LinkedHashMap) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) DirectMapMapping(org.eclipse.persistence.mappings.DirectMapMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) Vector(java.util.Vector) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JoinTable(com.blazebit.persistence.spi.JoinTable)

Example 7 with AggregateCollectionMapping

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

the class NoSubClassMatchTest_AggregateCollection method test.

@Override
public void test() {
    ((DatabaseSession) getSession()).addDescriptor(descriptor());
    AggregateCollectionMapping mapping = new AggregateCollectionMapping();
    mapping.setReferenceClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
    mapping.setSelectionSQLString("SELECT * FROM TABLE");
    mapping.initialize((AbstractSession) getSession());
    try {
        mapping.getReferenceDescriptor(NoSubClassMatchTest_AggregateCollection.class, (AbstractSession) getSession());
    } catch (EclipseLinkException exception) {
        caughtException = exception;
    }
}
Also used : AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException)

Example 8 with AggregateCollectionMapping

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

the class ObjectPersistenceRuntimeXMLProject method buildAggregateCollectionMappingDescriptor.

protected ClassDescriptor buildAggregateCollectionMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(AggregateCollectionMapping.class);
    descriptor.getInheritancePolicy().setParentClass(CollectionMapping.class);
    XMLCompositeCollectionMapping sourceToTargetKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    sourceToTargetKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashtables.
    sourceToTargetKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
            List<DatabaseField> sourceFields = mapping.getSourceKeyFields();
            List<DatabaseField> targetFields = mapping.getTargetForeignKeyFields();
            List<Association> associations = new ArrayList<>(sourceFields.size());
            for (int index = 0; index < sourceFields.size(); index++) {
                associations.add(new Association(targetFields.get(index), sourceFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setTargetForeignKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
                mapping.getTargetForeignKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "target-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
    relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
    descriptor.addMapping(relationshipPartnerAttributeNameMapping);
    XMLDirectMapping usesBatchReadingMapping = new XMLDirectMapping();
    usesBatchReadingMapping.setAttributeName("usesBatchReading");
    usesBatchReadingMapping.setGetMethodName("shouldUseBatchReading");
    usesBatchReadingMapping.setSetMethodName("setUsesBatchReading");
    usesBatchReadingMapping.setXPath(getPrimaryNamespaceXPath() + "batch-reading/text()");
    usesBatchReadingMapping.setNullValue(Boolean.FALSE);
    descriptor.addMapping(usesBatchReadingMapping);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("collectionPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    XMLCompositeObjectMapping indirectionPolicyMapping = new XMLCompositeObjectMapping();
    indirectionPolicyMapping.setReferenceClass(IndirectionPolicy.class);
    // Handle translation of NoIndirectionPolicy -> null.
    indirectionPolicyMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            IndirectionPolicy policy = ((ForeignReferenceMapping) object).getIndirectionPolicy();
            if (policy instanceof NoIndirectionPolicy) {
                return null;
            }
            return policy;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            IndirectionPolicy policy = (IndirectionPolicy) value;
            if (value == null) {
                policy = new NoIndirectionPolicy();
            }
            ((ForeignReferenceMapping) object).setIndirectionPolicy(policy);
        }
    });
    indirectionPolicyMapping.setAttributeName("indirectionPolicy");
    indirectionPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "indirection");
    descriptor.addMapping(indirectionPolicyMapping);
    XMLCompositeObjectMapping selectionQueryMapping = new XMLCompositeObjectMapping();
    selectionQueryMapping.setAttributeName("selectionQuery");
    selectionQueryMapping.setGetMethodName("getSelectionQuery");
    selectionQueryMapping.setSetMethodName("setSelectionQuery");
    selectionQueryMapping.setReferenceClass(ReadQuery.class);
    selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
    descriptor.addMapping(selectionQueryMapping);
    // delete-all query
    return descriptor;
}
Also used : ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) InMemoryQueryIndirectionPolicy(org.eclipse.persistence.queries.InMemoryQueryIndirectionPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) Association(org.eclipse.persistence.mappings.Association) PropertyAssociation(org.eclipse.persistence.mappings.PropertyAssociation) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 9 with AggregateCollectionMapping

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

the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappings.

// Create SQLDeleteAllStatements for mappings that may be responsible for references
// to the objects to be deleted
// in the tables NOT mapped to any class: ManyToManyMapping and DirectCollectionMapping
/**
 * NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappingsWithTempTable:
 *  if you are updating this method consider applying a similar update to that method as well.
 *
 * @return {@code Vector<SQLDeleteAllStatement>}
 */
protected Vector buildDeleteAllStatementsForMappings(SQLCall selectCallForExist, SQLSelectStatement selectStatementForExist, boolean dontCheckDescriptor) {
    Vector deleteStatements = new Vector();
    ClassDescriptor descriptor = getDescriptor();
    for (DatabaseMapping mapping : descriptor.getMappings()) {
        if (mapping.isForeignReferenceMapping()) {
            Vector sourceFields = null;
            Vector targetFields = null;
            if (mapping.isDirectCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
                }
            } else if (mapping.isAggregateCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
                }
            } else if (mapping.isManyToManyMapping()) {
                if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
                    RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
                    sourceFields = relationTableMechanism.getSourceKeyFields();
                    targetFields = relationTableMechanism.getSourceRelationKeyFields();
                }
            } else if (mapping.isOneToOneMapping()) {
                RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
                if (relationTableMechanism != null) {
                    if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
                        sourceFields = relationTableMechanism.getSourceKeyFields();
                        targetFields = relationTableMechanism.getSourceRelationKeyFields();
                    }
                }
            }
            if (sourceFields != null) {
                deleteStatements.add(buildDeleteAllStatementForMapping(selectCallForExist, selectStatementForExist, sourceFields, targetFields));
            }
        }
    }
    return deleteStatements;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 10 with AggregateCollectionMapping

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

the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappingsWithTempTable.

/**
 * Build delete statements with temporary table for ManyToMany and DirectCollection mappings.
 *
 * NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappings():
 *  if you are updating this method consider applying a similar update to that method as well.
 *
 * @return {@code Vector<SQLDeleteAllStatementForTempTable>}
 */
protected Vector buildDeleteAllStatementsForMappingsWithTempTable(ClassDescriptor descriptor, DatabaseTable rootTable, boolean dontCheckDescriptor) {
    Vector deleteStatements = new Vector();
    for (DatabaseMapping mapping : descriptor.getMappings()) {
        if (mapping.isForeignReferenceMapping()) {
            List<DatabaseField> sourceFields = null;
            List<DatabaseField> targetFields = null;
            if (mapping.isDirectCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
                }
            } else if (mapping.isAggregateCollectionMapping()) {
                if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
                    sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
                    targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
                }
            } else if (mapping.isManyToManyMapping()) {
                if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
                    RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
                    sourceFields = relationTableMechanism.getSourceKeyFields();
                    targetFields = relationTableMechanism.getSourceRelationKeyFields();
                }
            } else if (mapping.isOneToOneMapping()) {
                RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
                if (relationTableMechanism != null) {
                    if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
                        sourceFields = relationTableMechanism.getSourceKeyFields();
                        targetFields = relationTableMechanism.getSourceRelationKeyFields();
                    }
                }
            }
            if (sourceFields != null) {
                DatabaseTable targetTable = targetFields.get(0).getTable();
                SQLDeleteAllStatementForTempTable deleteStatement = buildDeleteAllStatementForTempTable(rootTable, sourceFields, targetTable, targetFields);
                deleteStatements.addElement(deleteStatement);
            }
        }
    }
    return deleteStatements;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) RelationTableMechanism(org.eclipse.persistence.mappings.RelationTableMechanism) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) SQLDeleteAllStatementForTempTable(org.eclipse.persistence.internal.expressions.SQLDeleteAllStatementForTempTable)

Aggregations

AggregateCollectionMapping (org.eclipse.persistence.mappings.AggregateCollectionMapping)10 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)6 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)4 Vector (java.util.Vector)3 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)2 DirectMapMapping (org.eclipse.persistence.mappings.DirectMapMapping)2 RelationTableMechanism (org.eclipse.persistence.mappings.RelationTableMechanism)2 JoinTable (com.blazebit.persistence.spi.JoinTable)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 Expression (org.eclipse.persistence.expressions.Expression)1 SQLDeleteAllStatementForTempTable (org.eclipse.persistence.internal.expressions.SQLDeleteAllStatementForTempTable)1