Search in sources :

Example 21 with CacheId

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

the class CachePolicy method indexObjectInCache.

/**
 * INTERNAL:
 * Index the object by index in the cache using the object.
 */
public void indexObjectInCache(CacheKey cacheKey, Object object, ClassDescriptor descriptor, AbstractSession session, boolean refresh) {
    if (!hasCacheIndexes()) {
        return;
    }
    for (CacheIndex index : this.cacheIndexes.values()) {
        if (!refresh || index.isUpdateable()) {
            List<DatabaseField> fields = index.getFields();
            int size = fields.size();
            Object[] values = new Object[size];
            for (int count = 0; count < size; count++) {
                values[count] = descriptor.getObjectBuilder().extractValueFromObjectForField(object, fields.get(count), session);
            }
            CacheId indexValues = new CacheId(values);
            session.getIdentityMapAccessorInstance().putCacheKeyByIndex(index, indexValues, cacheKey, descriptor);
        }
    }
}
Also used : CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 22 with CacheId

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

the class CachePolicy method indexObjectInCache.

/**
 * INTERNAL:
 * Index the object by index in the cache using its changeSet.
 */
public void indexObjectInCache(ObjectChangeSet changeSet, Object object, ClassDescriptor descriptor, AbstractSession session) {
    if (!hasCacheIndexes()) {
        return;
    }
    for (CacheIndex index : this.cacheIndexes.values()) {
        if ((changeSet == null) || (changeSet.isNew() && index.isInsertable()) || (!changeSet.isNew() && index.isUpdateable())) {
            List<DatabaseField> fields = index.getFields();
            int size = fields.size();
            Object[] values = new Object[size];
            for (int count = 0; count < size; count++) {
                values[count] = descriptor.getObjectBuilder().extractValueFromObjectForField(object, fields.get(count), session);
            }
            CacheId indexValues = new CacheId(values);
            CacheKey cacheKey = null;
            Object id = null;
            if (changeSet != null) {
                cacheKey = changeSet.getActiveCacheKey();
                if (cacheKey == null) {
                    id = changeSet.getId();
                }
            } else {
                id = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, session);
            }
            if (cacheKey == null) {
                cacheKey = session.getIdentityMapAccessorInstance().getCacheKeyForObject(id, descriptor.getJavaClass(), descriptor, true);
            }
            session.getIdentityMapAccessorInstance().putCacheKeyByIndex(index, indexValues, cacheKey, descriptor);
        }
    }
}
Also used : CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey)

Example 23 with CacheId

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

the class CachePolicy method indexObjectInCache.

/**
 * INTERNAL:
 * Index the object by index in the cache using its row.
 */
public void indexObjectInCache(CacheKey cacheKey, AbstractRecord databaseRow, Object domainObject, ClassDescriptor descriptor, AbstractSession session, boolean refresh) {
    if (!hasCacheIndexes()) {
        return;
    }
    for (CacheIndex index : this.cacheIndexes.values()) {
        if (!refresh || index.isUpdateable()) {
            List<DatabaseField> fields = index.getFields();
            int size = fields.size();
            Object[] values = new Object[size];
            for (int count = 0; count < size; count++) {
                values[count] = databaseRow.get(fields.get(count));
            }
            CacheId indexValues = new CacheId(values);
            session.getIdentityMapAccessorInstance().putCacheKeyByIndex(index, indexValues, cacheKey, descriptor);
        }
    }
}
Also used : CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 24 with CacheId

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

the class UnitOfWorkImpl method getReference.

/**
 * Get an instance, whose state may be lazily fetched.
 * If the requested instance does not exist in the database, null is returned, or the object will fail when accessed.
 * The instance will be lazy when it does not exist in the cache, and supports fetch groups.
 * @param id The primary key of the object, either as a List, singleton, IdClass or an instance of the object.
 */
@Override
public Object getReference(Class<?> theClass, Object id) {
    ClassDescriptor descriptor = getDescriptor(theClass);
    if (descriptor == null || descriptor.isDescriptorTypeAggregate()) {
        throw new IllegalArgumentException(ExceptionLocalization.buildMessage("unknown_bean_class", new Object[] { theClass }));
    }
    Object reference;
    if (id == null) {
        // gf721 - check for null PK
        throw new IllegalArgumentException(ExceptionLocalization.buildMessage("null_pk"));
    }
    Object primaryKey;
    if (id instanceof List) {
        if (descriptor.getCachePolicy().getCacheKeyType() == CacheKeyType.ID_VALUE) {
            if (((List) id).isEmpty()) {
                primaryKey = null;
            } else {
                primaryKey = ((List) id).get(0);
            }
        } else {
            primaryKey = new CacheId(((List) id).toArray());
        }
    } else if (id instanceof CacheId) {
        primaryKey = id;
    } else {
        if (descriptor.getCMPPolicy() != null) {
            if (descriptor.getCMPPolicy().getPKClass() != null && !descriptor.getCMPPolicy().getPKClass().isAssignableFrom(id.getClass())) {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { descriptor.getCMPPolicy().getPKClass(), id.getClass() }));
            }
            primaryKey = descriptor.getCMPPolicy().createPrimaryKeyFromId(id, this);
        } else {
            if (!id.getClass().equals(theClass)) {
                primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(id, this);
            } else {
                primaryKey = id;
            }
        }
    }
    // If the class supports fetch groups then return a un-fetched instance.
    if (descriptor.hasFetchGroupManager()) {
        reference = getIdentityMapAccessor().getFromIdentityMap(primaryKey, theClass);
        if (reference == null) {
            if ((id instanceof List) || (id instanceof CacheId) || (descriptor.getCMPPolicy() == null)) {
                AbstractRecord row = descriptor.getObjectBuilder().buildRowFromPrimaryKeyValues(primaryKey, this);
                reference = descriptor.getObjectBuilder().buildNewInstance();
                descriptor.getObjectBuilder().buildPrimaryKeyAttributesIntoObject(reference, row, new ReadObjectQuery(), this);
            } else {
                reference = descriptor.getCMPPolicy().createBeanUsingKey(id, this);
            }
            descriptor.getFetchGroupManager().getIdEntityFetchGroup().setOnEntity(reference, this);
            reference = registerExistingObject(reference);
        }
    } else {
        ReadObjectQuery query = new ReadObjectQuery(descriptor.getJavaClass());
        query.setSelectionId(primaryKey);
        query.conformResultsInUnitOfWork();
        query.setIsExecutionClone(true);
        reference = executeQuery(query);
    }
    return reference;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) List(java.util.List) ArrayList(java.util.ArrayList)

Example 25 with CacheId

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

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