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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations