Search in sources :

Example 21 with XMLObjectReferenceMapping

use of org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping 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 22 with XMLObjectReferenceMapping

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

the class NestedAttributeKeyProject method getEmployeeDescriptor.

private XMLDescriptor getEmployeeDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Employee.class);
    descriptor.setDefaultRootElement("employee");
    // create id mapping
    XMLDirectMapping idMapping = new XMLDirectMapping();
    idMapping.setAttributeName("id");
    idMapping.setXPath("@id");
    descriptor.addMapping(idMapping);
    // create name mapping
    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setXPath("name/text()");
    descriptor.addMapping(nameMapping);
    // create address mapping
    XMLObjectReferenceMapping addressMapping = new XMLObjectReferenceMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setReferenceClass(Address.class);
    addressMapping.addSourceToTargetKeyFieldAssociation("address-ref/@address-city", "city/text()");
    addressMapping.addSourceToTargetKeyFieldAssociation("address-ref/@address-zip", "zip/text()");
    descriptor.addMapping(addressMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)

Example 23 with XMLObjectReferenceMapping

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

the class CompositeNonStringKeyTypeProject method getEmployeeDescriptor.

private XMLDescriptor getEmployeeDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Employee.class);
    descriptor.setDefaultRootElement("employee");
    // create id mapping
    XMLDirectMapping idMapping = new XMLDirectMapping();
    idMapping.setAttributeName("id");
    idMapping.setXPath("@id");
    descriptor.addMapping(idMapping);
    // create name mapping
    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setXPath("name/text()");
    descriptor.addMapping(nameMapping);
    // create address mapping
    XMLObjectReferenceMapping addressMapping = new XMLObjectReferenceMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setReferenceClass(Address.class);
    addressMapping.addSourceToTargetKeyFieldAssociation("address-city/text()", "city/text()");
    addressMapping.addSourceToTargetKeyFieldAssociation("address-zip/text()", "zip/text()");
    descriptor.addMapping(addressMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)

Example 24 with XMLObjectReferenceMapping

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

the class ObjectRefClassProject method getCustomerDescriptor.

private XMLDescriptor getCustomerDescriptor() {
    XMLDescriptor xmlDescriptor = new XMLDescriptor();
    xmlDescriptor.setJavaClass(Customer.class);
    XMLObjectReferenceMapping addressMapping = new XMLObjectReferenceMapping();
    addressMapping.setAttributeName("address");
    addressMapping.addSourceToTargetKeyFieldAssociation("address-id/text()", null);
    addressMapping.setReferenceClass(Object.class);
    xmlDescriptor.addMapping(addressMapping);
    XMLCollectionReferenceMapping phoneNumbersMapping = new XMLCollectionReferenceMapping();
    phoneNumbersMapping.setAttributeName("phoneNumbers");
    phoneNumbersMapping.addSourceToTargetKeyFieldAssociation("phone-number-id/text()", null);
    phoneNumbersMapping.setReferenceClass(Object.class);
    phoneNumbersMapping.getContainerPolicy().setContainerClass(ArrayList.class);
    xmlDescriptor.addMapping(phoneNumbersMapping);
    return xmlDescriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)

Example 25 with XMLObjectReferenceMapping

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

the class SingleAttributeProject method getCustomerDescriptor.

private XMLDescriptor getCustomerDescriptor() {
    XMLDescriptor xmlDescriptor = new XMLDescriptor();
    xmlDescriptor.setJavaClass(Customer.class);
    XMLObjectReferenceMapping addressMapping = new XMLObjectReferenceMapping();
    addressMapping.setAttributeName("address");
    addressMapping.addSourceToTargetKeyFieldAssociation("address-id/text()", null);
    xmlDescriptor.addMapping(addressMapping);
    XMLCollectionReferenceMapping phoneNumbersMapping = new XMLCollectionReferenceMapping();
    phoneNumbersMapping.setAttributeName("phoneNumbers");
    phoneNumbersMapping.addSourceToTargetKeyFieldAssociation("@phone-number-id", null);
    phoneNumbersMapping.getContainerPolicy().setContainerClass(ArrayList.class);
    phoneNumbersMapping.setUsesSingleNode(true);
    xmlDescriptor.addMapping(phoneNumbersMapping);
    return xmlDescriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)

Aggregations

XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)35 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)32 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)20 XMLCollectionReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)14 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)12 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)8 XMLField (org.eclipse.persistence.oxm.XMLField)6 XMLSchemaClassPathReference (org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)6 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)2 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)2 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)2 XMLBinaryDataCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping)2 XMLBinaryDataMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping)2 XMLChoiceCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)2 XMLChoiceObjectMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)2 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1