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