Search in sources :

Example 1 with ReferenceResolver

use of org.eclipse.persistence.internal.oxm.ReferenceResolver in project eclipselink by eclipse-ee4j.

the class XMLObjectReferenceMapping method buildReference.

/**
 * INTERNAL:
 * Create (if necessary) and populate a reference object that will be used
 * during the mapping reference resolution phase after unmarshalling is
 * complete.
 */
@Override
public void buildReference(UnmarshalRecord record, XMLField xmlField, Object object, AbstractSession session) {
    ReferenceResolver resolver = record.getReferenceResolver();
    if (resolver == null) {
        return;
    }
    Object srcObject = record.getCurrentObject();
    // the order in which the primary keys are added to the vector is
    // relevant for cache lookup - it must match the ordering of the
    // reference descriptor's primary key entries
    Reference reference = resolver.getReference(this, srcObject);
    CacheId primaryKeys;
    if (null == referenceClass || ClassConstants.OBJECT == referenceClass) {
        if (reference == null) {
            // if reference is null, create a new instance and set it on the resolver
            primaryKeys = new CacheId(new Object[1]);
            reference = new Reference(this, srcObject, referenceClass, primaryKeys);
            resolver.addReference(reference);
            record.reference(reference);
        } else {
            primaryKeys = (CacheId) reference.getPrimaryKey();
        }
        primaryKeys.set(0, object);
    } else {
        Vector<String> pkFieldNames = referenceDescriptor.getPrimaryKeyFieldNames();
        // if reference is null, create a new instance and set it on the resolver
        if (reference == null) {
            primaryKeys = new CacheId(new Object[pkFieldNames.size()]);
            reference = new Reference(this, srcObject, referenceClass, primaryKeys);
            resolver.addReference(reference);
            record.reference(reference);
        } else {
            primaryKeys = (CacheId) reference.getPrimaryKey();
        }
        XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlField);
        int idx = pkFieldNames.indexOf(tgtFld.getQualifiedName());
        // fix for bug# 5687430
        // need to get the actual type of the target (i.e. int, String, etc.)
        // and use the converted value when checking the cache.
        Object value = session.getDatasourcePlatform().getConversionManager().convertObject(object, referenceDescriptor.getTypedField(tgtFld).getType());
        if (value != null) {
            primaryKeys.set(idx, value);
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) Reference(org.eclipse.persistence.internal.oxm.Reference) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) ReferenceResolver(org.eclipse.persistence.internal.oxm.ReferenceResolver)

Example 2 with ReferenceResolver

use of org.eclipse.persistence.internal.oxm.ReferenceResolver in project eclipselink by eclipse-ee4j.

the class XMLObjectReferenceMapping method readFromRowIntoObject.

/**
 * INTERNAL:
 * Extract the primary key values from the row, then create an
 * org.eclipse.persistence.internal.oxm.Reference instance and store it
 * on the session's org.eclipse.persistence.internal.oxm.ReferenceResolver.
 */
@Override
public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, CacheKey parentCacheKey, ObjectBuildingQuery sourceQuery, AbstractSession executionSession, boolean isTargetProtected) throws DatabaseException {
    // the order in which the primary keys are added to the vector is
    // relevant for cache lookup - it must match the ordering of the
    // reference descriptor's primary key entries
    CacheId primaryKeys;
    ClassDescriptor descriptor = sourceQuery.getSession().getClassDescriptor(referenceClass);
    Vector pkFieldNames = null;
    if (null == descriptor) {
        primaryKeys = new CacheId(new Object[1]);
    } else {
        pkFieldNames = descriptor.getPrimaryKeyFieldNames();
        primaryKeys = new CacheId(new Object[pkFieldNames.size()]);
    }
    Iterator keyIt = sourceToTargetKeys.iterator();
    while (keyIt.hasNext()) {
        XMLField keyFld = (XMLField) keyIt.next();
        XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(keyFld);
        Object value;
        int idx = 0;
        if (null == tgtFld) {
            value = databaseRow.get(keyFld);
        } else {
            idx = pkFieldNames.indexOf(tgtFld.getXPath());
            if (idx == -1) {
                continue;
            }
            // fix for bug# 5687430
            // need to get the actual type of the target (i.e. int, String, etc.)
            // and use the converted value when checking the cache.
            value = executionSession.getDatasourcePlatform().getConversionManager().convertObject(databaseRow.get(keyFld), descriptor.getTypedField(tgtFld).getType());
        }
        if (value != null) {
            primaryKeys.set(idx, value);
        }
    }
    // store the Reference instance on the resolver for use during mapping
    // resolution phase
    ReferenceResolver resolver = ((DOMRecord) databaseRow).getReferenceResolver();
    if (resolver != null) {
        resolver.addReference(new Reference(this, targetObject, referenceClass, primaryKeys));
    }
    return null;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Reference(org.eclipse.persistence.internal.oxm.Reference) ReferenceResolver(org.eclipse.persistence.internal.oxm.ReferenceResolver)

Aggregations

CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)2 Reference (org.eclipse.persistence.internal.oxm.Reference)2 ReferenceResolver (org.eclipse.persistence.internal.oxm.ReferenceResolver)2 XMLField (org.eclipse.persistence.oxm.XMLField)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)1