Search in sources :

Example 1 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 2 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)

Example 3 with CollectionMapping

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

the class OneToManyAccessor method processManyToManyMapping.

/**
 * INTERNAL:
 * Process an many to many mapping for this accessor since a join table
 * was specified.
 */
protected void processManyToManyMapping() {
    // Create a M-M mapping and process common collection mapping metadata
    // first followed by specific metadata.
    // Allow for different descriptor types (EIS) to create different mapping types.
    CollectionMapping mapping = getDescriptor().getClassDescriptor().newManyToManyMapping();
    process(mapping);
    if (mapping instanceof ManyToManyMapping) {
        // 266912: If this 1:n accessor is different than the n:n mapping - track this
        ((ManyToManyMapping) mapping).setDefinedAsOneToManyMapping(true);
        // Process the JoinTable metadata.
        processJoinTable(mapping, ((ManyToManyMapping) mapping).getRelationTableMechanism(), getJoinTableMetadata());
    } else if (mapping instanceof EISOneToManyMapping) {
        processEISOneToManyMapping((EISOneToManyMapping) mapping);
    }
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) EISOneToManyMapping(org.eclipse.persistence.eis.mappings.EISOneToManyMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 4 with CollectionMapping

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

the class DynamicXMLMetadataSource method createJAXBProperty.

/**
 * Create a JAXB property for a particular mapping.
 * This will only create JAXBProperties for mappings that are virtual - either because their
 * parent object is a dynamic class, or because the owning static class has virtual properties
 */
private JAXBElement<XmlElement> createJAXBProperty(DatabaseMapping mapping, ObjectFactory objectFactory, JavaType owningType, boolean isDynamic) {
    if (!mapping.getAttributeAccessor().isVirtualAttributeAccessor() && !isDynamic) {
        return null;
    }
    XmlElement xmlElement = new XmlElement();
    xmlElement.setJavaAttribute(mapping.getAttributeName());
    if (mapping.isObjectReferenceMapping()) {
        xmlElement.setType(((ObjectReferenceMapping) mapping).getReferenceClassName());
    } else if (mapping.isCollectionMapping()) {
        if (mapping.isEISMapping()) {
            // It will be fine for simple collections
            if (mapping instanceof EISCompositeDirectCollectionMapping) {
                xmlElement.setContainerType(mapping.getContainerPolicy().getContainerClassName());
            } else if (mapping instanceof EISCompositeCollectionMapping) {
                xmlElement.setContainerType(mapping.getContainerPolicy().getContainerClassName());
                xmlElement.setType(((EISCompositeCollectionMapping) mapping).getReferenceClassName());
            }
        } else {
            xmlElement.setType(((CollectionMapping) mapping).getReferenceClassName());
            xmlElement.setContainerType(mapping.getContainerPolicy().getContainerClassName());
        }
    } else {
        xmlElement.setType(mapping.getAttributeClassification().getName());
    }
    if (mapping.getAttributeAccessor().isVirtualAttributeAccessor()) {
        VirtualAttributeAccessor jpaAccessor = (VirtualAttributeAccessor) mapping.getAttributeAccessor();
        if (owningType.getXmlVirtualAccessMethods() == null) {
            XmlVirtualAccessMethods virtualAccessMethods = new XmlVirtualAccessMethods();
            virtualAccessMethods.setGetMethod(jpaAccessor.getGetMethodName());
            virtualAccessMethods.setSetMethod(jpaAccessor.getSetMethodName());
            owningType.setXmlVirtualAccessMethods(virtualAccessMethods);
        } else if (!owningType.getXmlVirtualAccessMethods().getGetMethod().equals(jpaAccessor.getGetMethodName())) {
            XmlAccessMethods accessMethods = new XmlAccessMethods();
            accessMethods.setGetMethod(jpaAccessor.getGetMethodName());
            accessMethods.setSetMethod(jpaAccessor.getSetMethodName());
            xmlElement.setXmlAccessMethods(accessMethods);
        }
    }
    return objectFactory.createXmlElement(xmlElement);
}
Also used : EISCompositeDirectCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeDirectCollectionMapping) XmlAccessMethods(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessMethods) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) EISCompositeCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeCollectionMapping) XmlElement(org.eclipse.persistence.jaxb.xmlmodel.XmlElement) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) EISCompositeCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeCollectionMapping) EISCompositeDirectCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeDirectCollectionMapping) XmlVirtualAccessMethods(org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethods)

Example 5 with CollectionMapping

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

the class UseTransparentMapOnCollectionMapping method setup.

@Override
protected void setup() {
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    descriptorToModify = project.getDescriptors().get(Employee.class);
    policy = new TransparentIndirectionPolicy();
    mapPolicy = new MapContainerPolicy();
    for (Enumeration<DatabaseMapping> mappingsEnum = (descriptorToModify.getMappings()).elements(); mappingsEnum.hasMoreElements(); ) {
        mappingToModify = mappingsEnum.nextElement();
        if (mappingToModify.isForeignReferenceMapping()) {
            if (mappingToModify.isCollectionMapping()) {
                CollectionMapping collectionMapping = (CollectionMapping) mappingToModify;
                collectionMapping.setContainerPolicy(mapPolicy);
                mapPolicy.setKeyName("testMethod");
                collectionMapping.getContainerPolicy().setContainerClass(Vector.class);
                ((ForeignReferenceMapping) mappingToModify).setIndirectionPolicy(policy);
            }
        }
    }
}
Also used : ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) 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