Search in sources :

Example 16 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildAggregateObjectMappingDescriptor.

protected ClassDescriptor buildAggregateObjectMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(AggregateObjectMapping.class);
    descriptor.getInheritancePolicy().setParentClass(AggregateMapping.class);
    XMLDirectMapping isNullAllowedMapping = new XMLDirectMapping();
    isNullAllowedMapping.setAttributeName("isNullAllowed");
    isNullAllowedMapping.setGetMethodName("isNullAllowed");
    isNullAllowedMapping.setSetMethodName("setIsNullAllowed");
    isNullAllowedMapping.setXPath(getPrimaryNamespaceXPath() + "allow-null/text()");
    isNullAllowedMapping.setNullValue(Boolean.TRUE);
    descriptor.addMapping(isNullAllowedMapping);
    XMLCompositeCollectionMapping aggregateToSourceFieldNameAssociationsMapping = new XMLCompositeCollectionMapping();
    aggregateToSourceFieldNameAssociationsMapping.setReferenceClass(FieldTranslation.class);
    // Handle translation of fields associations string to field.
    aggregateToSourceFieldNameAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            /*bug 322233: AttributeOverrides and AssociationOverride
                 * changed getAggregateToSourceFieldAssociations to hold String->DatabaseField associations
                 */
            AggregateObjectMapping mapping = (AggregateObjectMapping) object;
            List<Association> associations = mapping.getAggregateToSourceFieldAssociations();
            Vector<FieldTranslation> translations = new Vector<>(associations.size());
            for (int index = 0; index < associations.size(); index++) {
                Association association = associations.get(index);
                FieldTranslation translation = new FieldTranslation();
                translation.setKey(new DatabaseField((String) association.getKey()));
                translation.setValue(association.getValue());
                translations.add(translation);
            }
            return translations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            AggregateObjectMapping mapping = (AggregateObjectMapping) object;
            @SuppressWarnings({ "unchecked" }) Vector<Association> associations = (Vector<Association>) value;
            for (int index = 0; index < associations.size(); index++) {
                Association association = associations.get(index);
                association.setKey(((DatabaseField) association.getKey()).getQualifiedName());
            }
            mapping.setAggregateToSourceFieldAssociations(associations);
        }
    });
    aggregateToSourceFieldNameAssociationsMapping.setAttributeName("aggregateToSourceFieldNameAssociationsMapping");
    aggregateToSourceFieldNameAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "field-translations/" + getPrimaryNamespaceXPath() + "field-translation");
    descriptor.addMapping(aggregateToSourceFieldNameAssociationsMapping);
    return descriptor;
}
Also used : AggregateObjectMapping(org.eclipse.persistence.mappings.AggregateObjectMapping) 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) FieldTranslation(org.eclipse.persistence.internal.descriptors.FieldTranslation) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ArrayList(java.util.ArrayList) List(java.util.List) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 17 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildVariableOneToOneMappingDescriptor.

protected ClassDescriptor buildVariableOneToOneMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(VariableOneToOneMapping.class);
    descriptor.getInheritancePolicy().setParentClass(ObjectReferenceMapping.class);
    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);
    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);
    XMLCompositeObjectMapping typeFieldMapping = new XMLCompositeObjectMapping();
    typeFieldMapping.setAttributeName("typeField");
    typeFieldMapping.setGetMethodName("getTypeField");
    typeFieldMapping.setSetMethodName("setTypeField");
    typeFieldMapping.setReferenceClass(DatabaseField.class);
    typeFieldMapping.setXPath(getPrimaryNamespaceXPath() + "type-field");
    ((XMLField) typeFieldMapping.getField()).setLeafElementType(fieldQname);
    descriptor.addMapping(typeFieldMapping);
    XMLCompositeCollectionMapping foreignKeyFieldsMapping = new XMLCompositeCollectionMapping();
    foreignKeyFieldsMapping.useCollectionClass(NonSynchronizedVector.class);
    foreignKeyFieldsMapping.setAttributeName("foreignKeyFields");
    foreignKeyFieldsMapping.setGetMethodName("getForeignKeyFields");
    foreignKeyFieldsMapping.setSetMethodName("setForeignKeyFields");
    foreignKeyFieldsMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
    foreignKeyFieldsMapping.setReferenceClass(DatabaseField.class);
    ((XMLField) foreignKeyFieldsMapping.getField()).setLeafElementType(new QName(getSecondaryNamespace(), "field"));
    descriptor.addMapping(foreignKeyFieldsMapping);
    XMLCompositeCollectionMapping sourceFieldToTargetQueryKeyMapping = new XMLCompositeCollectionMapping();
    sourceFieldToTargetQueryKeyMapping.setAttributeName("sourceToTargetQueryKeyNames");
    sourceFieldToTargetQueryKeyMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-to-query-key/" + getPrimaryNamespaceXPath() + "query-key-reference");
    sourceFieldToTargetQueryKeyMapping.setGetMethodName("getSourceToTargetQueryKeyFieldAssociations");
    sourceFieldToTargetQueryKeyMapping.setSetMethodName("setSourceToTargetQueryKeyFieldAssociations");
    // Handle translation of query key associations string to field.
    sourceFieldToTargetQueryKeyMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            VariableOneToOneMapping mapping = (VariableOneToOneMapping) object;
            List<Association> associations = mapping.getSourceToTargetQueryKeyFieldAssociations();
            Vector<QueryKeyReference> queryKeyReferences = new Vector<>(associations.size());
            for (int index = 0; index < associations.size(); index++) {
                Association association = associations.get(index);
                QueryKeyReference reference = new QueryKeyReference();
                reference.setKey(new DatabaseField((String) association.getKey()));
                reference.setValue(association.getValue());
                queryKeyReferences.add(reference);
            }
            return queryKeyReferences;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            VariableOneToOneMapping mapping = (VariableOneToOneMapping) object;
            @SuppressWarnings({ "unchecked" }) Vector<Association> associations = (Vector<Association>) value;
            for (int index = 0; index < associations.size(); index++) {
                Association association = associations.get(index);
                association.setKey(((DatabaseField) association.getKey()).getQualifiedName());
            }
            mapping.setSourceToTargetQueryKeyFieldAssociations(associations);
        }
    });
    sourceFieldToTargetQueryKeyMapping.setReferenceClass(QueryKeyReference.class);
    descriptor.addMapping(sourceFieldToTargetQueryKeyMapping);
    XMLCompositeCollectionMapping classIndicatorsMapping = new XMLCompositeCollectionMapping();
    classIndicatorsMapping.setAttributeName("classIndicatorAssociations");
    classIndicatorsMapping.setGetMethodName("getClassIndicatorAssociations");
    classIndicatorsMapping.setSetMethodName("setClassIndicatorAssociations");
    classIndicatorsMapping.setXPath(getPrimaryNamespaceXPath() + "class-indicator-mappings/" + getPrimaryNamespaceXPath() + "class-indicator-mapping");
    classIndicatorsMapping.setReferenceClass(TypedAssociation.class);
    descriptor.addMapping(classIndicatorsMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QueryKeyReference(org.eclipse.persistence.internal.descriptors.QueryKeyReference) QName(javax.xml.namespace.QName) VariableOneToOneMapping(org.eclipse.persistence.mappings.VariableOneToOneMapping) 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) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) ArrayList(java.util.ArrayList) List(java.util.List) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 18 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildOneToManyMappingMappingDescriptor.

protected ClassDescriptor buildOneToManyMappingMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(OneToManyMapping.class);
    descriptor.getInheritancePolicy().setParentClass(CollectionMapping.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) {
            List<DatabaseField> sourceFields = ((OneToManyMapping) object).getSourceKeyFields();
            List<DatabaseField> targetFields = ((OneToManyMapping) object).getTargetForeignKeyFields();
            List<Association> associations = new ArrayList<>(sourceFields.size());
            for (int index = 0; index < sourceFields.size(); index++) {
                associations.add(new Association(targetFields.get(index), sourceFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            OneToManyMapping mapping = (OneToManyMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setTargetForeignKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
                mapping.getTargetForeignKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getSecondaryNamespaceXPath() + "target-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    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);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("collectionPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    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);
    // delete-all query
    return descriptor;
}
Also used : OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) EISOneToManyMapping(org.eclipse.persistence.eis.mappings.EISOneToManyMapping) 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) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 19 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildDirectCollectionMappingDescriptor.

protected ClassDescriptor buildDirectCollectionMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DirectCollectionMapping.class);
    descriptor.getInheritancePolicy().setParentClass(DatabaseMapping.class);
    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);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("containerPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    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);
    XMLDirectMapping referenceTableMapping = new XMLDirectMapping();
    referenceTableMapping.setAttributeName("referenceTableName");
    // CR#2407 Call getReferenceTableQualifiedName that includes table qualifier.
    referenceTableMapping.setGetMethodName("getReferenceTableQualifiedName");
    referenceTableMapping.setSetMethodName("setReferenceTableName");
    referenceTableMapping.setXPath(getPrimaryNamespaceXPath() + "reference-table/text()");
    descriptor.addMapping(referenceTableMapping);
    XMLCompositeObjectMapping directFieldMapping = new XMLCompositeObjectMapping();
    directFieldMapping.setAttributeName("directField");
    directFieldMapping.setGetMethodName("getDirectField");
    directFieldMapping.setSetMethodName("setDirectField");
    directFieldMapping.setXPath(getPrimaryNamespaceXPath() + "direct-field");
    directFieldMapping.setReferenceClass(DatabaseField.class);
    ((XMLField) directFieldMapping.getField()).setLeafElementType(fieldQname);
    descriptor.addMapping(directFieldMapping);
    XMLCompositeCollectionMapping sourceToReferenceKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
    sourceToReferenceKeyFieldAssociationsMapping.setReferenceClass(Association.class);
    // Handle translation of foreign key associations to hashtables.
    sourceToReferenceKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            List<DatabaseField> sourceFields = ((DirectCollectionMapping) object).getSourceKeyFields();
            List<DatabaseField> referenceFields = ((DirectCollectionMapping) object).getReferenceKeyFields();
            List<Association> associations = new ArrayList<>(sourceFields.size());
            for (int index = 0; index < sourceFields.size(); index++) {
                associations.add(new Association(referenceFields.get(index), sourceFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            DirectCollectionMapping mapping = (DirectCollectionMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setReferenceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
                mapping.getReferenceKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    sourceToReferenceKeyFieldAssociationsMapping.setAttributeName("sourceToReferenceKeyFieldAssociations");
    sourceToReferenceKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "reference-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToReferenceKeyFieldAssociationsMapping);
    XMLCompositeObjectMapping valueConverterMapping = new XMLCompositeObjectMapping();
    valueConverterMapping.setAttributeName("valueConverter");
    valueConverterMapping.setGetMethodName("getValueConverter");
    valueConverterMapping.setSetMethodName("setValueConverter");
    valueConverterMapping.setXPath(getPrimaryNamespaceXPath() + "value-converter");
    valueConverterMapping.setReferenceClass(Converter.class);
    descriptor.addMapping(valueConverterMapping);
    XMLCompositeObjectMapping historyPolicyMapping = new XMLCompositeObjectMapping();
    historyPolicyMapping.setAttributeName("historyPolicy");
    historyPolicyMapping.setGetMethodName("getHistoryPolicy");
    historyPolicyMapping.setSetMethodName("setHistoryPolicy");
    historyPolicyMapping.setReferenceClass(HistoryPolicy.class);
    historyPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "history-policy");
    descriptor.addMapping(historyPolicyMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) 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) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) EISCompositeDirectCollectionMapping(org.eclipse.persistence.eis.mappings.EISCompositeDirectCollectionMapping) AbstractCompositeDirectCollectionMapping(org.eclipse.persistence.mappings.foundation.AbstractCompositeDirectCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) 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) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) ArrayList(java.util.ArrayList) List(java.util.List) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 20 with Association

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

the class ObjectPersistenceRuntimeXMLProject method buildAggregateCollectionMappingDescriptor.

protected ClassDescriptor buildAggregateCollectionMappingDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(AggregateCollectionMapping.class);
    descriptor.getInheritancePolicy().setParentClass(CollectionMapping.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) {
            AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
            List<DatabaseField> sourceFields = mapping.getSourceKeyFields();
            List<DatabaseField> targetFields = mapping.getTargetForeignKeyFields();
            List<Association> associations = new ArrayList<>(sourceFields.size());
            for (int index = 0; index < sourceFields.size(); index++) {
                associations.add(new Association(targetFields.get(index), sourceFields.get(index)));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
            @SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
            mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            mapping.setTargetForeignKeyFields(NonSynchronizedVector.newInstance(associations.size()));
            Iterator<Association> iterator = associations.iterator();
            while (iterator.hasNext()) {
                Association association = iterator.next();
                mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
                mapping.getTargetForeignKeyFields().add((DatabaseField) association.getKey());
            }
        }
    });
    sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
    sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "target-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
    descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
    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);
    XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
    containerPolicyMapping.setAttributeName("collectionPolicy");
    containerPolicyMapping.setGetMethodName("getContainerPolicy");
    containerPolicyMapping.setSetMethodName("setContainerPolicy");
    containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
    containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
    descriptor.addMapping(containerPolicyMapping);
    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);
    // delete-all query
    return descriptor;
}
Also used : 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) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) 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) ArrayList(java.util.ArrayList) List(java.util.List) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

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