Search in sources :

Example 21 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildOneToOneMappingDescriptor.

protected ClassDescriptor buildOneToOneMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(OneToOneMapping.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 = ((OneToOneMapping) 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) {
            OneToOneMapping mapping = (OneToOneMapping) 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(getSecondaryNamespaceXPath() + "foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    XMLCompositeCollectionMapping foreignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
    foreignKeyFieldNamesMapping.useCollectionClass(NonSynchronizedVector.class);
    foreignKeyFieldNamesMapping.setAttributeName("foreignKeyFields");
    foreignKeyFieldNamesMapping.setGetMethodName("getForeignKeyFields");
    foreignKeyFieldNamesMapping.setSetMethodName("setForeignKeyFields");
    foreignKeyFieldNamesMapping.setXPath(getSecondaryNamespaceXPath() + "foreign-key-fields/" + getSecondaryNamespaceXPath() + "field");
    foreignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
    ((XMLField) foreignKeyFieldNamesMapping.getField()).setLeafElementType(fieldQname);
    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);
    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);
    XMLDirectMapping joinFetchMapping = new XMLDirectMapping();
    joinFetchMapping.setAttributeName("joinFetch");
    joinFetchMapping.setXPath(getPrimaryNamespaceXPath() + "join-fetch/text()");
    ObjectTypeConverter joinFetchConverter = new ObjectTypeConverter();
    joinFetchConverter.addConversionValue("true", ForeignReferenceMapping.INNER_JOIN);
    joinFetchConverter.addConversionValue("false", ForeignReferenceMapping.NONE);
    joinFetchMapping.setConverter(joinFetchConverter);
    joinFetchMapping.setNullValue(ForeignReferenceMapping.NONE);
    descriptor.addMapping(joinFetchMapping);
    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);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) HashMap(java.util.HashMap) 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) 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) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Map(java.util.Map) HardCacheWeakIdentityMap(org.eclipse.persistence.internal.identitymaps.HardCacheWeakIdentityMap) CacheIdentityMap(org.eclipse.persistence.internal.identitymaps.CacheIdentityMap) NoIdentityMap(org.eclipse.persistence.internal.identitymaps.NoIdentityMap) WeakIdentityMap(org.eclipse.persistence.internal.identitymaps.WeakIdentityMap) FullIdentityMap(org.eclipse.persistence.internal.identitymaps.FullIdentityMap) SoftCacheWeakIdentityMap(org.eclipse.persistence.internal.identitymaps.SoftCacheWeakIdentityMap) HashMap(java.util.HashMap) EISOneToOneMapping(org.eclipse.persistence.eis.mappings.EISOneToOneMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) VariableOneToOneMapping(org.eclipse.persistence.mappings.VariableOneToOneMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 22 with Association

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

the class ObjectPersistenceRuntimeXMLProject_11_1_1 method buildXMLObjectReferenceMappingDescriptor.

protected ClassDescriptor buildXMLObjectReferenceMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(XMLObjectReferenceMapping.class);
    descriptor.getInheritancePolicy().setParentClass(AggregateMapping.class);
    XMLCompositeCollectionMapping sourceToTargetKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    sourceToTargetKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashmaps.
    sourceToTargetKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            Map<XMLField, XMLField> sourceToTargetKeyFields = ((XMLObjectReferenceMapping) object).getSourceToTargetKeyFieldAssociations();
            List<Association> associations = new ArrayList<>(sourceToTargetKeyFields.size());
            Iterator<Map.Entry<XMLField, XMLField>> iterator = sourceToTargetKeyFields.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<XMLField, XMLField> entry = iterator.next();
                associations.add(new Association(entry.getKey(), entry.getValue()));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            XMLObjectReferenceMapping mapping = (XMLObjectReferenceMapping) object;
            List<Association> associations = (List<Association>) value;
            mapping.setSourceToTargetKeyFieldAssociations(new HashMap<>(associations.size() + 1));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceToTargetKeyFieldAssociations().put(association.getKey(), association.getValue());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "source-to-target-key-field-association/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    XMLCompositeCollectionMapping sourceToTargetKeysMapping = new XMLCompositeCollectionMapping();
    sourceToTargetKeysMapping.setReferenceClass(DatabaseField.class);
    sourceToTargetKeysMapping.setAttributeName("sourceToTargetKeys");
    sourceToTargetKeysMapping.setXPath(getPrimaryNamespaceXPath() + "source-to-target-key-fields/" + getPrimaryNamespaceXPath() + "field");
    ((XMLField) sourceToTargetKeysMapping.getField()).setLeafElementType(fieldQname);
    descriptor.addMapping(sourceToTargetKeysMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) XMLChoiceFieldToClassAssociation(org.eclipse.persistence.internal.oxm.XMLChoiceFieldToClassAssociation) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) SoftIdentityMap(org.eclipse.persistence.internal.identitymaps.SoftIdentityMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with Association

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

the class AbstractTransformationMapping method getFieldNameToMethodNameAssociations.

/**
 * INTERNAL:
 * Needed for backwards compatibility
 */
public Vector getFieldNameToMethodNameAssociations() {
    Vector associations = new Vector();
    for (Iterator<FieldTransformation> source = getFieldTransformations().iterator(); source.hasNext(); ) {
        FieldTransformation tf = source.next();
        if (tf instanceof MethodBasedFieldTransformation) {
            Association ass = new Association();
            ass.setKey(tf.getField().getQualifiedName());
            ass.setValue(((MethodBasedFieldTransformation) tf).getMethodName());
            associations.addElement(ass);
        }
    }
    return associations;
}
Also used : Association(org.eclipse.persistence.mappings.Association) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation) Vector(java.util.Vector) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Example 24 with Association

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

the class AbstractTransformationMapping method setFieldNameToMethodNameAssociations.

/**
 * INTERNAL:
 * needed for backwards compatibility
 */
public void setFieldNameToMethodNameAssociations(Vector associations) {
    setFieldTransformations(org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(associations.size()));
    for (Iterator source = associations.iterator(); source.hasNext(); ) {
        Association ass = (Association) source.next();
        MethodBasedFieldTransformation tf = new MethodBasedFieldTransformation();
        tf.setField(new DatabaseField((String) ass.getKey()));
        tf.setMethodName((String) ass.getValue());
        getFieldTransformations().add(tf);
    }
}
Also used : Association(org.eclipse.persistence.mappings.Association) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator)

Example 25 with Association

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

the class DBWSBuilderModelProject method buildDBWSBuilderModelDescriptor.

protected ClassDescriptor buildDBWSBuilderModelDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DBWSBuilderModel.class);
    descriptor.setDefaultRootElement("dbws-builder");
    XMLCompositeCollectionMapping propertiesMapping = new XMLCompositeCollectionMapping();
    propertiesMapping.setReferenceClass(Association.class);
    propertiesMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public String getAttributeName() {
            return "properties";
        }

        @Override
        public Object getAttributeValueFromObject(Object object) throws DescriptorException {
            DBWSBuilderModel model = (DBWSBuilderModel) object;
            Vector<Association> associations = new Vector<Association>();
            for (Map.Entry<String, String> me : model.properties.entrySet()) {
                associations.add(new Association(me.getKey(), me.getValue()));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
            DBWSBuilderModel model = (DBWSBuilderModel) object;
            Vector<Association> associations = (Vector<Association>) value;
            for (Association a : associations) {
                model.properties.put((String) a.getKey(), (String) a.getValue());
            }
        }
    });
    propertiesMapping.setXPath("properties/property");
    descriptor.addMapping(propertiesMapping);
    XMLChoiceCollectionMapping operationsMapping = new XMLChoiceCollectionMapping();
    operationsMapping.setAttributeName("operations");
    operationsMapping.setContainerPolicy(new ListContainerPolicy(ArrayList.class));
    operationsMapping.addChoiceElement("table", TableOperationModel.class);
    operationsMapping.addChoiceElement("procedure", ProcedureOperationModel.class);
    operationsMapping.addChoiceElement("plsql-procedure", PLSQLProcedureOperationModel.class);
    operationsMapping.addChoiceElement("sql", SQLOperationModel.class);
    operationsMapping.addChoiceElement("batch-sql", BatchSQLOperationModel.class);
    descriptor.addMapping(operationsMapping);
    return descriptor;
}
Also used : DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ListContainerPolicy(org.eclipse.persistence.internal.queries.ListContainerPolicy) ArrayList(java.util.ArrayList) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Vector(java.util.Vector)

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