use of org.eclipse.persistence.mappings.Association in project eclipselink by eclipse-ee4j.
the class EISObjectPersistenceXMLProject method buildEISOneToOneMappingDescriptor.
protected ClassDescriptor buildEISOneToOneMappingDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(EISOneToOneMapping.class);
descriptor.getInheritancePolicy().setParentClass(ObjectReferenceMapping.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) {
Map<DatabaseField, DatabaseField> sourceToTargetKeyFields = ((EISOneToOneMapping) object).getSourceToTargetKeyFields();
List<Association> associations = new ArrayList<>(sourceToTargetKeyFields.size());
Iterator<Map.Entry<DatabaseField, DatabaseField>> iterator = sourceToTargetKeyFields.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<DatabaseField, DatabaseField> entry = iterator.next();
associations.add(new Association(entry.getKey(), entry.getValue()));
}
return associations;
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
EISOneToOneMapping mapping = (EISOneToOneMapping) object;
@SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
mapping.setSourceToTargetKeyFields(new HashMap<>(associations.size() + 1));
mapping.setTargetToSourceKeyFields(new HashMap<>(associations.size() + 1));
Iterator<Association> iterator = associations.iterator();
while (iterator.hasNext()) {
Association association = iterator.next();
mapping.getSourceToTargetKeyFields().put((DatabaseField) association.getKey(), (DatabaseField) association.getValue());
mapping.getTargetToSourceKeyFields().put((DatabaseField) association.getValue(), (DatabaseField) association.getKey());
}
}
});
sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key/" + getPrimaryNamespaceXPath() + "field-reference");
descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
XMLCompositeCollectionMapping foreignKeyFieldNamesMapping = new XMLCompositeCollectionMapping();
foreignKeyFieldNamesMapping.setAttributeName("foreignKeyFields");
foreignKeyFieldNamesMapping.setGetMethodName("getForeignKeyFields");
foreignKeyFieldNamesMapping.setSetMethodName("setForeignKeyFields");
foreignKeyFieldNamesMapping.setXPath(getPrimaryNamespaceXPath() + "foreign-key-fields/" + getPrimaryNamespaceXPath() + "field");
foreignKeyFieldNamesMapping.setReferenceClass(DatabaseField.class);
descriptor.addMapping(foreignKeyFieldNamesMapping);
XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
descriptor.addMapping(relationshipPartnerAttributeNameMapping);
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.setReferenceClass(ReadQuery.class);
selectionQueryMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
if (((ForeignReferenceMapping) object).hasCustomSelectionQuery()) {
return ((ForeignReferenceMapping) object).getSelectionQuery();
}
return null;
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
if (value instanceof ReadQuery) {
((ForeignReferenceMapping) object).setCustomSelectionQuery((ReadQuery) value);
}
}
});
selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
descriptor.addMapping(selectionQueryMapping);
return descriptor;
}
use of org.eclipse.persistence.mappings.Association in project eclipselink by eclipse-ee4j.
the class ClassDescriptor method getMultipleTableForeignKeyAssociations.
/**
* INTERNAL:
* Returns the foreign key relationships used for multiple tables which were specified by the user. Used
* by the Project XML writer to output these associations
*
* @see #adjustMultipleTableInsertOrder()
*/
public Vector getMultipleTableForeignKeyAssociations() {
Vector associations = new Vector(getAdditionalTablePrimaryKeyFields().size() * 2);
Iterator<Map<DatabaseField, DatabaseField>> tablesHashtable = getAdditionalTablePrimaryKeyFields().values().iterator();
while (tablesHashtable.hasNext()) {
Map<DatabaseField, DatabaseField> tableHash = tablesHashtable.next();
Iterator<DatabaseField> fieldEnumeration = tableHash.keySet().iterator();
while (fieldEnumeration.hasNext()) {
DatabaseField keyField = fieldEnumeration.next();
// PRS#36802(CR#2057) contains() is changed to containsKey()
if (getMultipleTableForeignKeys().containsKey(keyField.getTable())) {
Association association = new Association(keyField.getQualifiedName(), tableHash.get(keyField).getQualifiedName());
associations.addElement(association);
}
}
}
return associations;
}
use of org.eclipse.persistence.mappings.Association in project eclipselink by eclipse-ee4j.
the class InheritancePolicy method getClassIndicatorAssociations.
/**
* INTERNAL:
* Return the class indicator associations for XML.
* List of class-name/value associations.
*/
public Vector<Association> getClassIndicatorAssociations() {
Vector<Association> associations = new Vector<>(getClassNameIndicatorMapping().size() / 2);
Iterator classesEnum = getClassNameIndicatorMapping().keySet().iterator();
Iterator valuesEnum = getClassNameIndicatorMapping().values().iterator();
while (classesEnum.hasNext()) {
Object className = classesEnum.next();
// If the project was built in runtime is a class, MW is a string.
if (className instanceof Class) {
className = ((Class<?>) className).getName();
}
Object value = valuesEnum.next();
associations.addElement(new TypedAssociation(className, value));
}
return associations;
}
use of org.eclipse.persistence.mappings.Association in project eclipselink by eclipse-ee4j.
the class MapEntryDirectEntity1MReportQueryTest method buildExpectedResults.
@Override
protected void buildExpectedResults() {
Vector holders = getSession().readAllObjects(DirectEntity1MMapHolder.class);
for (Enumeration e = holders.elements(); e.hasMoreElements(); ) {
DirectEntity1MMapHolder holder = (DirectEntity1MMapHolder) e.nextElement();
Iterator<Map.Entry> i = holder.getDirectToEntityMap().entrySet().iterator();
while (i.hasNext()) {
Object[] result = new Object[1];
Map.Entry entry = i.next();
result[0] = new Association(entry.getKey(), entry.getValue());
addResult(result, null);
}
}
}
use of org.eclipse.persistence.mappings.Association in project eclipselink by eclipse-ee4j.
the class MapContainerPolicy method nextEntry.
/**
* INTERNAL:
* Return the next object on the queue. The iterator is the one
* returned from #iteratorFor().
*
* This will return a MapEntry to allow use of the key
*
* @see ContainerPolicy#iteratorFor(Object)
* @see #unwrapIteratorResult(Object)
*/
@Override
public Object nextEntry(Object iterator, AbstractSession session) {
Map.Entry next = (Map.Entry) nextEntry(iterator);
Object object = next.getValue();
if (hasElementDescriptor()) {
object = getElementDescriptor().getObjectBuilder().unwrapObject(object, session);
}
Object key = next.getKey();
key = unwrapKey(key, session);
next = new Association(key, object);
return next;
}
Aggregations