Search in sources :

Example 6 with CollectionMapping

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

the class QueryKeyExpression method getLeafMapping.

/**
 * INTERNAL:
 * Lookup the mapping for this item by traversing its expression recursively.
 * If an aggregate of foreign mapping is found it is traversed.
 */
@Override
public DatabaseMapping getLeafMapping(DatabaseQuery query, ClassDescriptor rootDescriptor, AbstractSession session) {
    if (isMapEntryExpression()) {
        MapEntryExpression mapEntryExpression = (MapEntryExpression) this;
        // get the expression that we want the table entry for
        QueryKeyExpression baseExpression = (QueryKeyExpression) mapEntryExpression.getBaseExpression();
        // get the expression that owns the mapping for the table entry
        Expression owningExpression = baseExpression.getBaseExpression();
        ClassDescriptor owningDescriptor = owningExpression.getLeafDescriptor(query, rootDescriptor, session);
        // Get the mapping that owns the table
        CollectionMapping mapping = (CollectionMapping) owningDescriptor.getObjectBuilder().getMappingForAttributeName(baseExpression.getName());
        if (mapEntryExpression.shouldReturnMapEntry()) {
            return mapping;
        }
        if (mapping.getContainerPolicy().isMappedKeyMapPolicy()) {
            MappedKeyMapContainerPolicy policy = (MappedKeyMapContainerPolicy) mapping.getContainerPolicy();
            return (DatabaseMapping) policy.getKeyMapping();
        }
        return mapping;
    }
    Expression baseExpression = getBaseExpression();
    ClassDescriptor descriptor = baseExpression.getLeafDescriptor(query, rootDescriptor, session);
    if (descriptor == null) {
        return null;
    }
    return descriptor.getObjectBuilder().getMappingForAttributeName(getName());
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Expression(org.eclipse.persistence.expressions.Expression) MappedKeyMapContainerPolicy(org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 7 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 8 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 9 with CollectionMapping

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

the class ManyToManyAccessor method process.

/**
 * INTERNAL:
 * Process a many to many metadata accessor into a EclipseLink
 * ManyToManyMapping.
 */
@Override
public void process() {
    super.process();
    // Create a M-M mapping and process common collection mapping metadata.
    // Allow for different descriptor types (EIS) to create different mapping types.
    CollectionMapping mapping = getDescriptor().getClassDescriptor().newManyToManyMapping();
    process(mapping);
    if (hasMappedBy()) {
        // We are processing the non-owning side of a M-M relationship. Get
        // the owning mapping from the reference descriptor metadata and
        // process the keys from it.
        DatabaseMapping owningMapping = getOwningMapping();
        if (owningMapping.isManyToManyMapping()) {
            ManyToManyMapping ownerMapping = (ManyToManyMapping) owningMapping;
            processMappedByRelationTable(ownerMapping.getRelationTableMechanism(), ((ManyToManyMapping) mapping).getRelationTableMechanism());
            // Set the mapping read-only.
            mapping.setIsReadOnly(true);
        } else {
            // Invalid owning mapping type, throw an exception.
            throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
        }
    } else if (mapping instanceof ManyToManyMapping) {
        // Processing the owning side of a M-M, process the join table.
        processJoinTable(mapping, ((ManyToManyMapping) mapping).getRelationTableMechanism(), getJoinTableMetadata());
    } else if (mapping instanceof EISOneToManyMapping) {
        processEISOneToManyMapping((EISOneToManyMapping) mapping);
    }
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) EISOneToManyMapping(org.eclipse.persistence.eis.mappings.EISOneToManyMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 10 with CollectionMapping

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

the class MappingAccessor method setIndirectionPolicy.

/**
 * INTERNAL:
 * Set the correct indirection policy on a collection mapping. Method
 * assume that the reference class has been set on the mapping before
 * calling this method.
 */
protected void setIndirectionPolicy(ContainerMapping mapping, String mapKey, boolean usesIndirection) {
    MetadataClass rawClass = getRawClass();
    boolean containerPolicySet = false;
    if (usesIndirection && (mapping instanceof ForeignReferenceMapping)) {
        containerPolicySet = true;
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        if (rawClass.isClass(Map.class)) {
            if (collectionMapping.isDirectMapMapping()) {
                ((DirectMapMapping) mapping).useTransparentMap();
            } else {
                collectionMapping.useTransparentMap(mapKey);
            }
        } else if (rawClass.isClass(List.class)) {
            collectionMapping.useTransparentList();
        } else if (rawClass.isClass(Collection.class)) {
            collectionMapping.useTransparentCollection();
        } else if (rawClass.isClass(Set.class)) {
            collectionMapping.useTransparentSet();
        } else {
            getLogger().logWarningMessage(MetadataLogger.WARNING_INVALID_COLLECTION_USED_ON_LAZY_RELATION, getJavaClass(), getAnnotatedElement(), rawClass);
            processIndirection((ForeignReferenceMapping) mapping);
            containerPolicySet = false;
        }
    } else {
        if (mapping instanceof CollectionMapping) {
            ((CollectionMapping) mapping).dontUseIndirection();
        }
    }
    if (!containerPolicySet) {
        if (rawClass.isClass(Map.class)) {
            if (mapping instanceof DirectMapMapping) {
                ((DirectMapMapping) mapping).useMapClass(java.util.Hashtable.class);
            } else {
                mapping.useMapClass(java.util.Hashtable.class, mapKey);
            }
        } else if (rawClass.isClass(Set.class)) {
            // This will cause it to use a CollectionContainerPolicy type
            mapping.useCollectionClass(java.util.HashSet.class);
        } else if (rawClass.isClass(List.class)) {
            // This will cause a ListContainerPolicy type to be used or
            // OrderedListContainerPolicy if ordering is specified.
            mapping.useCollectionClass(java.util.Vector.class);
        } else if (rawClass.isClass(Collection.class)) {
            // Force CollectionContainerPolicy type to be used with a
            // collection implementation.
            mapping.setContainerPolicy(new CollectionContainerPolicy(java.util.Vector.class));
        } else {
            // Use the supplied collection class type if its not an interface
            if (mapKey == null || mapKey.equals("")) {
                if (rawClass.isList()) {
                    mapping.useListClassName(rawClass.getName());
                } else {
                    mapping.useCollectionClassName(rawClass.getName());
                }
            } else {
                mapping.useMapClassName(rawClass.getName(), mapKey);
            }
        }
    }
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) Set(java.util.Set) MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) DirectMapMapping(org.eclipse.persistence.mappings.DirectMapMapping)

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