Search in sources :

Example 11 with CollectionMapping

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

the class ProjectClassGenerator method addForeignReferenceMappingLines.

protected void addForeignReferenceMappingLines(NonreflectiveMethodDefinition method, String mappingName, ForeignReferenceMapping mapping) {
    if (mapping.getReferenceClassName() != null) {
        method.addLine(mappingName + ".setReferenceClass(" + mapping.getReferenceClassName() + ".class);");
    }
    if (mapping.getRelationshipPartnerAttributeName() != null) {
        method.addLine(mappingName + ".setRelationshipPartnerAttributeName(\"" + mapping.getRelationshipPartnerAttributeName() + "\");");
    }
    IndirectionPolicy policy = mapping.getIndirectionPolicy();
    if (policy instanceof ContainerIndirectionPolicy) {
        String containerClassName = ((ContainerIndirectionPolicy) policy).getContainerClassName();
        method.addLine(mappingName + ".useContainerIndirection(" + containerClassName + ".class);");
    // Bug#4251902 used in ObjectReferenceMapping
    } else if (policy instanceof ProxyIndirectionPolicy) {
        method.addLine(mappingName + ".useProxyIndirection();");
    } else if (policy instanceof BasicIndirectionPolicy) {
        method.addLine(mappingName + ".useBasicIndirection();");
    } else if (policy instanceof NoIndirectionPolicy) {
        method.addLine(mappingName + ".dontUseIndirection();");
    }
    if (mapping.shouldUseBatchReading()) {
        method.addLine(mappingName + ".useBatchReading();");
    }
    if (mapping.isJoinFetched()) {
        if (mapping.isInnerJoinFetched()) {
            method.addLine(mappingName + ".useInnerJoinFetch();");
        } else if (mapping.isOuterJoinFetched()) {
            method.addLine(mappingName + ".useOuterJoinFetch();");
        }
    }
    if ((!mapping.isDirectCollectionMapping()) && mapping.isPrivateOwned()) {
        method.addLine(mappingName + ".privateOwnedRelationship();");
    }
    if (mapping.isCollectionMapping()) {
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        String collectionClassName = collectionMapping.getContainerPolicy().getContainerClassName();
        if (mapping.getContainerPolicy().isCollectionPolicy()) {
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentCollection();");
            }
            if (!collectionClassName.equals(Vector.class.getName())) {
                method.addLine(mappingName + ".useCollectionClass(" + collectionClassName + ".class);");
            }
        } else if (collectionMapping.isDirectMapMapping()) {
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentMap();");
                if (!collectionClassName.equals(IndirectMap.class.getName())) {
                    method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
                }
            } else {
                method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
            }
        } else if (collectionMapping.getContainerPolicy().isMapPolicy()) {
            String keyMethodName = ((org.eclipse.persistence.internal.queries.MapContainerPolicy) collectionMapping.getContainerPolicy()).getKeyName();
            if (policy instanceof TransparentIndirectionPolicy) {
                method.addLine(mappingName + ".useTransparentMap(\"" + keyMethodName + "\");");
                if (!collectionClassName.equals(IndirectMap.class.getName())) {
                    method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
                }
            } else {
                method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
            }
        }
        // Ordering.
        Iterator<Expression> queryKeyExpressions = collectionMapping.getOrderByQueryKeyExpressions().iterator();
        while (queryKeyExpressions.hasNext()) {
            FunctionExpression expression = (FunctionExpression) queryKeyExpressions.next();
            String queryKeyName = expression.getBaseExpression().getName();
            if (expression.getOperator().getSelector() == ExpressionOperator.Descending) {
                method.addLine(mappingName + ".addDescendingOrdering(\"" + queryKeyName + "\");");
            } else {
                method.addLine(mappingName + ".addAscendingOrdering(\"" + queryKeyName + "\");");
            }
        }
    }
}
Also used : ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) 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) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) IndirectMap(org.eclipse.persistence.indirection.IndirectMap) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) Expression(org.eclipse.persistence.expressions.Expression) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping)

Example 12 with CollectionMapping

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

the class EntityManagerJUnitTestCase method testLazyListInstantiationEager.

// bug 325035
public void testLazyListInstantiationEager() {
    EntityManager em = createEntityManager();
    CollectionMapping mapping = ((CollectionMapping) getServerSession().getProject().getClassDescriptor(Company.class).getMappingForAttributeName("vehicles"));
    Boolean lazyIndirection = mapping.shouldUseLazyInstantiationForIndirectCollection();
    mapping.setUseLazyInstantiationForIndirectCollection(false);
    beginTransaction(em);
    Company company = new Company();
    company.setName("ListCo");
    em.persist(company);
    Car car = new Car();
    em.persist(car);
    company.getVehicles().add(car);
    car.setOwner(company);
    commitTransaction(em);
    clearCache();
    try {
        em = createEntityManager();
        company = em.find(Company.class, company.getId());
        company.getVehicles().add(new Car());
        assertTrue("Lazy instantiation was not disabled for IndirectList.", ((IndirectList) company.getVehicles()).getAddedElements().size() == 0);
    } finally {
        em = createEntityManager();
        beginTransaction(em);
        company = em.find(Company.class, company.getId());
        car = em.find(Car.class, car.getId());
        em.remove(company);
        em.remove(car);
        commitTransaction(em);
        mapping.setUseLazyInstantiationForIndirectCollection(lazyIndirection);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Company(org.eclipse.persistence.testing.models.jpa.inheritance.Company) Car(org.eclipse.persistence.testing.models.jpa.inheritance.Car) SportsCar(org.eclipse.persistence.testing.models.jpa.inheritance.SportsCar) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 13 with CollectionMapping

use of org.eclipse.persistence.mappings.CollectionMapping 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 14 with CollectionMapping

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

the class EmployeeSystem method addDescriptors.

@Override
public void addDescriptors(DatabaseSession session) {
    if (shouldOverrideContainerPolicy) {
        List<CollectionMapping> listOrderMappings = ((EmployeeProject) this.project).getListOrderMappings();
        for (int i = 0; i < listOrderMappings.size(); i++) {
            CollectionMapping mapping = listOrderMappings.get(i);
            mapping.setContainerPolicy(new NullsLastOrderedListContainerPolicy(mapping.getContainerPolicy().getContainerClass()));
        }
    }
    session.addDescriptors(project);
}
Also used : CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 15 with CollectionMapping

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

the class ProjectXMLOrderByQueryKeysTest method test.

@Override
public void test() {
    Project project = new EmployeeProject();
    CollectionMapping mapping = (CollectionMapping) project.getDescriptor(Employee.class).getMappingForAttributeName(ATTRIBUTE_NAME);
    // Add the new order by query keys to the mapping
    mapping.addAscendingOrdering("ascending1");
    mapping.addDescendingOrdering("descending1");
    mapping.addAscendingOrdering("ascending2");
    mapping.addDescendingOrdering("descending2");
    m_queryKeyExpressionsBeforeWrite = mapping.getOrderByQueryKeyExpressions();
    // Write out the project with changes and read back in again.
    XMLProjectWriter.write(TEMP_FILE, project);
    project = XMLProjectReader.read(TEMP_FILE, getClass().getClassLoader());
    mapping = (CollectionMapping) project.getDescriptor(Employee.class).getMappingForAttributeName(ATTRIBUTE_NAME);
    // Store the query keys after reading them back in again
    m_queryKeyExpressionsAfterWrite = mapping.getOrderByQueryKeyExpressions();
}
Also used : Project(org.eclipse.persistence.sessions.Project) EmployeeProject(org.eclipse.persistence.testing.models.employee.relational.EmployeeProject) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) EmployeeProject(org.eclipse.persistence.testing.models.employee.relational.EmployeeProject) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Aggregations

CollectionMapping (org.eclipse.persistence.mappings.CollectionMapping)27 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)11 DirectCollectionMapping (org.eclipse.persistence.mappings.DirectCollectionMapping)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)4 Expression (org.eclipse.persistence.expressions.Expression)3 AggregateCollectionMapping (org.eclipse.persistence.mappings.AggregateCollectionMapping)3 EntityManager (jakarta.persistence.EntityManager)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 EISCompositeCollectionMapping (org.eclipse.persistence.eis.mappings.EISCompositeCollectionMapping)2 EISOneToManyMapping (org.eclipse.persistence.eis.mappings.EISOneToManyMapping)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 TransparentIndirectionPolicy (org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 MapContainerPolicy (org.eclipse.persistence.internal.queries.MapContainerPolicy)2 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)2 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)2