Search in sources :

Example 26 with XMLCollectionReferenceMapping

use of org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping 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
    XMLCollectionReferenceMapping addressMapping = new XMLCollectionReferenceMapping();
    addressMapping.useCollectionClass(ArrayList.class);
    addressMapping.setAttributeName("addresses");
    addressMapping.setReferenceClass(Address.class);
    addressMapping.addSourceToTargetKeyFieldAssociation("addresses/address-ref/@address-id", "@aid");
    addressMapping.setUsesSingleNode(false);
    descriptor.addMapping(addressMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)

Example 27 with XMLCollectionReferenceMapping

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

use of org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping 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)

Example 29 with XMLCollectionReferenceMapping

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

the class MappingsGenerator method generateXMLCollectionReferenceMapping.

/**
 * Create an XMLCollectionReferenceMapping and add it to the descriptor.
 */
public CollectionReferenceMapping generateXMLCollectionReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass referenceClass) {
    CollectionReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, UnmarshalRecord, XMLField, XMLRecord> mapping = new XMLCollectionReferenceMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    mapping.setUsesSingleNode(property.isXmlList() || (property.isAttribute() && (property.getXmlPath() == null || !property.getXmlPath().contains("/"))));
    String referenceClassName = referenceClass.getQualifiedName();
    JavaClass collectionType = property.getType();
    if (collectionType.isArray()) {
        JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
        JavaClass componentType = collectionType.getComponentType();
        if (componentType.isArray()) {
            Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getName());
            referenceClassName = adaptedClass.getName();
            accessor.setAdaptedClassName(referenceClassName);
            JavaClass baseComponentType = getBaseComponentType(componentType);
            if (baseComponentType.isPrimitive()) {
                Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
                accessor.setComponentClass(primitiveClass);
            } else {
                accessor.setComponentClassName(baseComponentType.getQualifiedName());
            }
        } else {
            accessor.setComponentClassName(componentType.getQualifiedName());
        }
        mapping.setAttributeAccessor(accessor);
    }
    collectionType = containerClassImpl(collectionType);
    mapping.useCollectionClassName(collectionType.getRawName());
    mapping.setReferenceClassName(referenceClassName);
    // here we need to setup source/target key field associations
    if (property.isSetXmlJoinNodes()) {
        for (XmlJoinNode xmlJoinNode : property.getXmlJoinNodes().getXmlJoinNode()) {
            validateJoinNode(descriptor.getJavaClassName(), property, xmlJoinNode.getReferencedXmlPath(), referenceClass);
            mapping.addSourceToTargetKeyFieldAssociation(xmlJoinNode.getXmlPath(), xmlJoinNode.getReferencedXmlPath());
        }
    } else {
        // here we need to setup source/target key field associations
        TypeInfo referenceType = typeInfo.get(referenceClass.getQualifiedName());
        String tgtXPath = null;
        if (null != referenceType && referenceType.isIDSet()) {
            Property prop = referenceType.getIDProperty();
            tgtXPath = getXPathForField(prop, namespaceInfo, !prop.isAttribute(), false).getXPath();
        }
        // if the XPath is set (via xml-path) use it
        Field srcXPath;
        if (property.getXmlPath() != null) {
            srcXPath = new XMLField(property.getXmlPath());
        } else {
            srcXPath = getXPathForField(property, namespaceInfo, true, false);
        }
        mapping.addSourceToTargetKeyFieldAssociation(srcXPath.getXPath(), tgtXPath);
    }
    if (property.getInverseReferencePropertyName() != null) {
        mapping.getInverseReferenceMapping().setAttributeName(property.getInverseReferencePropertyName());
        JavaClass backPointerPropertyType = null;
        if (property.getInverseReferencePropertyGetMethodName() != null && property.getInverseReferencePropertySetMethodName() != null && !property.getInverseReferencePropertyGetMethodName().equals("") && !property.getInverseReferencePropertySetMethodName().equals("")) {
            mapping.getInverseReferenceMapping().setGetMethodName(property.getInverseReferencePropertySetMethodName());
            mapping.getInverseReferenceMapping().setSetMethodName(property.getInverseReferencePropertySetMethodName());
            JavaMethod getMethod = referenceClass.getDeclaredMethod(mapping.getInverseReferenceMapping().getGetMethodName(), new JavaClass[] {});
            if (getMethod != null) {
                backPointerPropertyType = getMethod.getReturnType();
            }
        } else {
            JavaField backpointerField = referenceClass.getDeclaredField(property.getInverseReferencePropertyName());
            if (backpointerField != null) {
                backPointerPropertyType = backpointerField.getResolvedType();
            }
        }
        if (helper.isCollectionType(backPointerPropertyType)) {
            mapping.getInverseReferenceMapping().setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 30 with XMLCollectionReferenceMapping

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

the class OppositeProject method getChild1Descriptor.

private XMLDescriptor getChild1Descriptor() {
    XMLDescriptor xmlDescriptor = new XMLDescriptor();
    xmlDescriptor.setJavaClass(Child1.class);
    xmlDescriptor.addPrimaryKeyFieldName("@id");
    NamespaceResolver namespaceResolver = new NamespaceResolver();
    namespaceResolver.put("tns", "urn:opposite");
    xmlDescriptor.setNamespaceResolver(namespaceResolver);
    XMLSchemaClassPathReference schemaReference = new XMLSchemaClassPathReference();
    schemaReference.setSchemaContext("/tns:child1");
    schemaReference.setType(XMLSchemaReference.COMPLEX_TYPE);
    xmlDescriptor.setSchemaReference(schemaReference);
    XMLDirectMapping idMapping = new XMLDirectMapping();
    idMapping.setAttributeName("id");
    idMapping.setXPath("@id");
    xmlDescriptor.addMapping(idMapping);
    XMLObjectReferenceMapping child2Mapping = new XMLObjectReferenceMapping();
    child2Mapping.setReferenceClass(Child2.class);
    child2Mapping.setAttributeName("child2");
    child2Mapping.addSourceToTargetKeyFieldAssociation("tns:child2/text()", "@id");
    xmlDescriptor.addMapping(child2Mapping);
    XMLCollectionReferenceMapping child2CollectionMapping = new XMLCollectionReferenceMapping();
    child2CollectionMapping.setReferenceClass(Child2.class);
    child2CollectionMapping.setAttributeName("child2Collection");
    child2CollectionMapping.addSourceToTargetKeyFieldAssociation("tns:child2collection/text()", "@id");
    xmlDescriptor.addMapping(child2CollectionMapping);
    return xmlDescriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLSchemaClassPathReference(org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)

Aggregations

XMLCollectionReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)34 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)32 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)20 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)14 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)12 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)7 XMLSchemaClassPathReference (org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)4 XMLField (org.eclipse.persistence.oxm.XMLField)3 ArrayList (java.util.ArrayList)2 XMLAnyObjectMapping (org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)2 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)2 JAXBException (jakarta.xml.bind.JAXBException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)1 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)1