Search in sources :

Example 11 with Association

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

the class EISObjectPersistenceXMLProject method buildEISOneToOneMappingDescriptor.

protected ClassDescriptor buildEISOneToOneMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(EISOneToOneMapping.class);
    descriptor.getInheritancePolicy().setParentClass(ObjectReferenceMapping.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) {
            Map<DatabaseField, DatabaseField> sourceToTargetKeyFields = ((EISOneToOneMapping) object).getSourceToTargetKeyFields();
            List<Association> associations = new ArrayList<>(sourceToTargetKeyFields.size());
            Iterator<Map.Entry<DatabaseField, DatabaseField>> iterator = sourceToTargetKeyFields.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<DatabaseField, DatabaseField> entry = iterator.next();
                associations.add(new Association(entry.getKey(), entry.getValue()));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            EISOneToOneMapping mapping = (EISOneToOneMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceToTargetKeyFields(new HashMap<>(associations.size() + 1));
            mapping.setTargetToSourceKeyFields(new HashMap<>(associations.size() + 1));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceToTargetKeyFields().put((DatabaseField) association.getKey(), (DatabaseField) association.getValue());
                mapping.getTargetToSourceKeyFields().put((DatabaseField) association.getValue(), (DatabaseField) association.getKey());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key/" + getPrimaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    XMLCompositeCollectionMapping foreignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
    foreignKeyFieldNamesMapping.setAttributeName("foreignKeyFields");
    foreignKeyFieldNamesMapping.setGetMethodName("getForeignKeyFields");
    foreignKeyFieldNamesMapping.setSetMethodName("setForeignKeyFields");
    foreignKeyFieldNamesMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
    foreignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
    descriptor.addMapping(foreignKeyFieldNamesMapping);
    XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
    relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
    relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
    descriptor.addMapping(relationshipPartnerAttributeNameMapping);
    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.setReferenceClass(ReadQuery.class);
    selectionQueryMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            if (((ForeignReferenceMapping) object).hasCustomSelectionQuery()) {
                return ((ForeignReferenceMapping) object).getSelectionQuery();
            }
            return null;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            if (value instanceof ReadQuery) {
                ((ForeignReferenceMapping) object).setCustomSelectionQuery((ReadQuery) value);
            }
        }
    });
    selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
    descriptor.addMapping(selectionQueryMapping);
    return descriptor;
}
Also used : HashMap(java.util.HashMap) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) 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) EISOneToOneMapping(org.eclipse.persistence.eis.mappings.EISOneToOneMapping) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) HashMap(java.util.HashMap) Map(java.util.Map) ReadQuery(org.eclipse.persistence.queries.ReadQuery) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 12 with Association

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

the class ClassDescriptor method getMultipleTableForeignKeyAssociations.

/**
 * INTERNAL:
 * Returns the foreign key relationships used for multiple tables which were specified by the user. Used
 * by the Project XML writer to output these associations
 *
 * @see #adjustMultipleTableInsertOrder()
 */
public Vector getMultipleTableForeignKeyAssociations() {
    Vector associations = new Vector(getAdditionalTablePrimaryKeyFields().size() * 2);
    Iterator<Map<DatabaseField, DatabaseField>> tablesHashtable = getAdditionalTablePrimaryKeyFields().values().iterator();
    while (tablesHashtable.hasNext()) {
        Map<DatabaseField, DatabaseField> tableHash = tablesHashtable.next();
        Iterator<DatabaseField> fieldEnumeration = tableHash.keySet().iterator();
        while (fieldEnumeration.hasNext()) {
            DatabaseField keyField = fieldEnumeration.next();
            // PRS#36802(CR#2057) contains() is changed to containsKey()
            if (getMultipleTableForeignKeys().containsKey(keyField.getTable())) {
                Association association = new Association(keyField.getQualifiedName(), tableHash.get(keyField).getQualifiedName());
                associations.addElement(association);
            }
        }
    }
    return associations;
}
Also used : Association(org.eclipse.persistence.mappings.Association) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Map(java.util.Map) IdentityMap(org.eclipse.persistence.internal.identitymaps.IdentityMap) HashMap(java.util.HashMap)

Example 13 with Association

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

the class InheritancePolicy method getClassIndicatorAssociations.

/**
 * INTERNAL:
 * Return the class indicator associations for XML.
 * List of class-name/value associations.
 */
public Vector<Association> getClassIndicatorAssociations() {
    Vector<Association> associations = new Vector<>(getClassNameIndicatorMapping().size() / 2);
    Iterator classesEnum = getClassNameIndicatorMapping().keySet().iterator();
    Iterator valuesEnum = getClassNameIndicatorMapping().values().iterator();
    while (classesEnum.hasNext()) {
        Object className = classesEnum.next();
        // If the project was built in runtime is a class, MW is a string.
        if (className instanceof Class) {
            className = ((Class<?>) className).getName();
        }
        Object value = valuesEnum.next();
        associations.addElement(new TypedAssociation(className, value));
    }
    return associations;
}
Also used : Association(org.eclipse.persistence.mappings.Association) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) TypedAssociation(org.eclipse.persistence.mappings.TypedAssociation) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 14 with Association

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

the class MapEntryDirectEntity1MReportQueryTest method buildExpectedResults.

@Override
protected void buildExpectedResults() {
    Vector holders = getSession().readAllObjects(DirectEntity1MMapHolder.class);
    for (Enumeration e = holders.elements(); e.hasMoreElements(); ) {
        DirectEntity1MMapHolder holder = (DirectEntity1MMapHolder) e.nextElement();
        Iterator<Map.Entry> i = holder.getDirectToEntityMap().entrySet().iterator();
        while (i.hasNext()) {
            Object[] result = new Object[1];
            Map.Entry entry = i.next();
            result[0] = new Association(entry.getKey(), entry.getValue());
            addResult(result, null);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) Association(org.eclipse.persistence.mappings.Association) DirectEntity1MMapHolder(org.eclipse.persistence.testing.models.collections.map.DirectEntity1MMapHolder) Vector(java.util.Vector) Map(java.util.Map)

Example 15 with Association

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

the class MapContainerPolicy method nextEntry.

/**
 * INTERNAL:
 * Return the next object on the queue. The iterator is the one
 * returned from #iteratorFor().
 *
 * This will return a MapEntry to allow use of the key
 *
 * @see ContainerPolicy#iteratorFor(Object)
 * @see #unwrapIteratorResult(Object)
 */
@Override
public Object nextEntry(Object iterator, AbstractSession session) {
    Map.Entry next = (Map.Entry) nextEntry(iterator);
    Object object = next.getValue();
    if (hasElementDescriptor()) {
        object = getElementDescriptor().getObjectBuilder().unwrapObject(object, session);
    }
    Object key = next.getKey();
    key = unwrapKey(key, session);
    next = new Association(key, object);
    return next;
}
Also used : Entry(java.util.Map.Entry) Entry(java.util.Map.Entry) Association(org.eclipse.persistence.mappings.Association) Map(java.util.Map)

Aggregations

Association (org.eclipse.persistence.mappings.Association)25 ArrayList (java.util.ArrayList)13 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)13 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)12 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)12 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)12 Iterator (java.util.Iterator)11 List (java.util.List)11 TypedAssociation (org.eclipse.persistence.mappings.TypedAssociation)11 Vector (java.util.Vector)10 PropertyAssociation (org.eclipse.persistence.mappings.PropertyAssociation)8 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IndirectionPolicy (org.eclipse.persistence.internal.indirection.IndirectionPolicy)7 NoIndirectionPolicy (org.eclipse.persistence.internal.indirection.NoIndirectionPolicy)7 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)7 BasicIndirectionPolicy (org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy)6 ContainerIndirectionPolicy (org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy)6 ProxyIndirectionPolicy (org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy)6