Search in sources :

Example 26 with CacheId

use of org.eclipse.persistence.internal.identitymaps.CacheId 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)

Example 27 with CacheId

use of org.eclipse.persistence.internal.identitymaps.CacheId in project eclipselink by eclipse-ee4j.

the class ReferenceResolver method createPKVectorsFromMap.

/**
 * INTERNAL:
 * Create primary key values to be used for cache lookup.  The map
 * of primary keys on the reference is keyed on the reference descriptors primary
 * key field names.  Each of these primary keys contains all of the values for a
 * particular key - in the order that they we read in from the document.  For
 * example, if the key field names are A, B, and C, and there are three reference
 * object instances, then the hashmap would have the following:
 * (A=[1,2,3], B=[X,Y,Z], C=[Jim, Joe, Jane]).  If the primary key field names on
 * the reference descriptor contained [B, C, A], then the result of this method call
 * would be reference.primaryKeys=([X, Jim, 1], [Y, Joe, 2], [Z, Jane, 3]).
 */
private void createPKVectorsFromMap(final Reference reference, final CollectionReferenceMapping mapping) {
    final CoreDescriptor referenceDescriptor = mapping.getReferenceDescriptor();
    final Vector<CacheId> pks = new Vector<>();
    if (null == referenceDescriptor) {
        final CacheId pkVals = (CacheId) reference.getPrimaryKeyMap().get(null);
        if (null == pkVals) {
            return;
        }
        for (int x = 0; x < pkVals.getPrimaryKey().length; x++) {
            final Object[] values = new Object[1];
            values[0] = pkVals.getPrimaryKey()[x];
            pks.add(new CacheId(values));
        }
    } else {
        final List pkFields = referenceDescriptor.getPrimaryKeyFieldNames();
        if (pkFields.isEmpty()) {
            return;
        }
        boolean init = true;
        // for each primary key field name
        for (Object pkField : pkFields) {
            final CacheId pkVals = (CacheId) reference.getPrimaryKeyMap().get(pkField);
            if (pkVals == null) {
                return;
            }
            // initialize the list of pk vectors once and only once
            if (init) {
                for (int i = 0; i < pkVals.getPrimaryKey().length; i++) {
                    pks.add(new CacheId(new Object[0]));
                }
                init = false;
            }
            // now add each value for the current target key to it's own vector
            for (int i = 0; i < pkVals.getPrimaryKey().length; i++) {
                final Object val = pkVals.getPrimaryKey()[i];
                (pks.get(i)).add(val);
            }
        }
    }
    reference.setPrimaryKey(pks);
}
Also used : CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector)

Example 28 with CacheId

use of org.eclipse.persistence.internal.identitymaps.CacheId in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method writeIntoRowFromPrimaryKeyValues.

/**
 * Build the row from the primary key values.
 */
public AbstractRecord writeIntoRowFromPrimaryKeyValues(AbstractRecord row, Object primaryKey, AbstractSession session, boolean convert) {
    List<DatabaseField> primaryKeyFields = this.descriptor.getPrimaryKeyFields();
    if (this.descriptor.getCachePolicy().getCacheKeyType() == CacheKeyType.ID_VALUE) {
        DatabaseField field = primaryKeyFields.get(0);
        Object value = primaryKey;
        value = session.getPlatform(this.descriptor.getJavaClass()).getConversionManager().convertObject(value, field.getType());
        row.put(field, value);
        return row;
    }
    int size = primaryKeyFields.size();
    Object[] primaryKeyValues = ((CacheId) primaryKey).getPrimaryKey();
    for (int index = 0; index < size; index++) {
        DatabaseField field = primaryKeyFields.get(index);
        Object value = primaryKeyValues[index];
        value = session.getPlatform(this.descriptor.getJavaClass()).getConversionManager().convertObject(value, field.getType());
        row.put(field, value);
    }
    return row;
}
Also used : CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject)

Example 29 with CacheId

use of org.eclipse.persistence.internal.identitymaps.CacheId in project eclipselink by eclipse-ee4j.

the class QueryKeyExpression method getFieldValue.

/**
 * INTERNAL:
 * Transform the object-level value into a database-level value
 */
@Override
public Object getFieldValue(Object objectValue, AbstractSession session) {
    DatabaseMapping mapping = getMapping();
    Object fieldValue = objectValue;
    if (mapping != null) {
        if (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping()) {
            // CR#3623207, check for IN Collection here not in mapping.
            if (objectValue instanceof Collection) {
                // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
                Collection values = (Collection) objectValue;
                Vector fieldValues = new Vector(values.size());
                for (Iterator iterator = values.iterator(); iterator.hasNext(); ) {
                    Object value = iterator.next();
                    if (!(value instanceof Expression)) {
                        value = getFieldValue(value, session);
                    }
                    fieldValues.add(value);
                }
                fieldValue = fieldValues;
            } else {
                if (mapping.isAbstractColumnMapping()) {
                    fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(objectValue, session);
                } else if (mapping.isDirectCollectionMapping()) {
                    fieldValue = ((DirectCollectionMapping) mapping).getFieldValue(objectValue, session);
                }
            }
        } else if ((objectValue instanceof Collection) && (mapping.isForeignReferenceMapping())) {
            // Was an IN with a collection of objects, extract their ids.
            List ids = new ArrayList();
            for (Object object : ((Collection) objectValue)) {
                if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
                    Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
                    if (id instanceof CacheId) {
                        id = Arrays.asList(((CacheId) id).getPrimaryKey());
                    }
                    ids.add(id);
                } else {
                    ids.add(object);
                }
            }
            fieldValue = ids;
        }
    }
    return fieldValue;
}
Also used : Expression(org.eclipse.persistence.expressions.Expression) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping)

Example 30 with CacheId

use of org.eclipse.persistence.internal.identitymaps.CacheId in project eclipselink by eclipse-ee4j.

the class RelationTableMechanism method extractBatchKeyFromRow.

/**
 * INTERNAL:
 * Extract the foreign key value from the source row.
 */
protected Object extractBatchKeyFromRow(AbstractRecord row, AbstractSession session) {
    Object[] key;
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    List<DatabaseField> sourceKeyFields = this.sourceKeyFields;
    int size = sourceKeyFields.size();
    key = new Object[size];
    for (int index = 0; index < size; index++) {
        DatabaseField field = sourceKeyFields.get(index);
        Object value = row.get(field);
        // Must ensure the classification gets a cache hit.
        key[index] = conversionManager.convertObject(value, field.getType());
    }
    return new CacheId(key);
}
Also used : CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Aggregations

CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)38 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)19 ArrayList (java.util.ArrayList)12 List (java.util.List)10 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)8 ConversionException (org.eclipse.persistence.exceptions.ConversionException)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)5 HashMap (java.util.HashMap)4 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)4 XMLField (org.eclipse.persistence.oxm.XMLField)4 Vector (java.util.Vector)3 CacheKeyType (org.eclipse.persistence.annotations.CacheKeyType)3 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)3 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)3 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)3 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3