Search in sources :

Example 31 with CacheId

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

the class RelationTableMechanism method extractKeyFromTargetRow.

/**
 * INTERNAL:
 * Extract the source primary key value from the relation row.
 * Used for batch reading, most following same order and fields as in the mapping.
 */
protected Object extractKeyFromTargetRow(AbstractRecord row, AbstractSession session) {
    int size = getSourceRelationKeyFields().size();
    Object[] key = new Object[size];
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    for (int index = 0; index < size; index++) {
        DatabaseField relationField = this.sourceRelationKeyFields.get(index);
        DatabaseField sourceField = this.sourceKeyFields.get(index);
        Object value = row.get(relationField);
        // Must ensure the classification gets a cache hit.
        value = conversionManager.convertObject(value, sourceField.getType());
        key[index] = value;
    }
    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)

Example 32 with CacheId

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

the class XMLObjectBuilder method buildObject.

/**
 * INTERNAL: Override the parent's buildObject to allow for the caching of
 * aggregate objects in OX. By caching aggregates along with XML Nodes that
 * they were created from, we are able to preserve the structure and
 * unmapped content of the document that was used to create these objects.
 */
@Override
public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
    XMLRecord row = (XMLRecord) databaseRow;
    row.setSession(query.getSession());
    XMLUnmarshaller unmarshaller = row.getUnmarshaller();
    Object parent = row.getOwningObject();
    if (!(isXmlDescriptor() || getDescriptor().isDescriptorTypeAggregate())) {
        return super.buildObject(query, databaseRow, joinManager);
    }
    query.getSession().startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    ClassDescriptor concreteDescriptor = getDescriptor();
    Object domainObject = null;
    // in the mapping
    if (concreteDescriptor.hasInheritance() && (parent == null)) {
        // look for an xsi:type attribute in the xml document
        InheritancePolicy inheritancePolicy = concreteDescriptor.getInheritancePolicy();
        Class<?> classValue = inheritancePolicy.classFromRow(databaseRow, query.getSession());
        if ((classValue == null) && isXmlDescriptor()) {
            // no xsi:type attribute - look for type indicator on the
            // default root element
            QName leafElementType = ((Descriptor) concreteDescriptor).getDefaultRootElementType();
            // inheritance policy
            if (leafElementType != null) {
                XPathQName xpathQName = new XPathQName(leafElementType, row.isNamespaceAware());
                Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
                if (indicator != null) {
                    classValue = (Class) indicator;
                }
            }
        }
        // class, if non-abstract
        if (classValue != null) {
            concreteDescriptor = query.getSession().getDescriptor(classValue);
            if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) {
                concreteDescriptor = getDescriptor();
            }
            if (concreteDescriptor == null) {
                throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue);
            }
        } else {
            // make sure the class is non-abstract
            if (Modifier.isAbstract(concreteDescriptor.getJavaClass().getModifiers())) {
                // throw an exception
                throw DescriptorException.missingClassIndicatorField(databaseRow, inheritancePolicy.getDescriptor());
            }
        }
    }
    domainObject = concreteDescriptor.getObjectBuilder().buildNewInstance();
    row.setCurrentObject(domainObject);
    if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
        unmarshaller.getUnmarshalListener().beforeUnmarshal(domainObject, parent);
    }
    concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, null, databaseRow, query, joinManager, null, false, query.getSession());
    if (isXmlDescriptor() && ((Descriptor) concreteDescriptor).getPrimaryKeyFieldNames().size() > 0) {
        Object pk = extractPrimaryKeyFromRow(databaseRow, query.getSession());
        if ((pk != null) && (((CacheId) pk).getPrimaryKey().length > 0)) {
            DOMRecord domRecord = (DOMRecord) databaseRow;
            domRecord.getReferenceResolver().putValue(concreteDescriptor.getJavaClass(), pk, domainObject);
        }
    }
    DocumentPreservationPolicy docPresPolicy = row.getDocPresPolicy();
    if (docPresPolicy != null) {
        // EIS XML Cases won't have a doc pres policy set
        row.getDocPresPolicy().addObjectToCache(domainObject, row.getDOM());
    }
    query.getSession().endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
        unmarshaller.getUnmarshalListener().afterUnmarshal(domainObject, parent);
    }
    return domainObject;
}
Also used : InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) QName(javax.xml.namespace.QName) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) DocumentPreservationPolicy(org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 33 with CacheId

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

the class DirectCollectionMapping method extractKeyFromTargetRow.

/**
 * INTERNAL:
 * Extract the source primary key value from the reference direct row.
 * Used for batch reading, most following same order and fields as in the mapping.
 */
@Override
protected Object extractKeyFromTargetRow(AbstractRecord row, AbstractSession session) {
    int size = this.referenceKeyFields.size();
    Object[] key = new Object[size];
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    for (int index = 0; index < size; index++) {
        DatabaseField relationField = this.referenceKeyFields.get(index);
        DatabaseField sourceField = this.sourceKeyFields.get(index);
        Object value = row.get(relationField);
        // Must ensure the classification gets a cache hit.
        try {
            value = conversionManager.convertObject(value, sourceField.getType());
        } catch (ConversionException e) {
            throw ConversionException.couldNotBeConverted(this, getDescriptor(), e);
        }
        key[index] = value;
    }
    return new CacheId(key);
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Example 34 with CacheId

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

the class DirectCollectionMapping method extractBatchKeyFromRow.

/**
 * INTERNAL:
 * Extract the primary key value from the source row.
 * Used for batch reading, most following same order and fields as in the mapping.
 */
@Override
protected Object extractBatchKeyFromRow(AbstractRecord row, AbstractSession session) {
    int size = this.sourceKeyFields.size();
    Object[] key = new Object[size];
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    for (int index = 0; index < size; index++) {
        DatabaseField field = this.sourceKeyFields.get(index);
        Object value = row.get(field);
        // Must ensure the classification to get a cache hit.
        try {
            value = conversionManager.convertObject(value, field.getType());
        } catch (ConversionException exception) {
            throw ConversionException.couldNotBeConverted(this, this.descriptor, exception);
        }
        key[index] = value;
    }
    return new CacheId(key);
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Example 35 with CacheId

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

the class OneToManyMapping method extractBatchKeyFromRow.

/**
 * Extract the key field values from the specified row.
 * Used for batch reading. Keep the fields in the same order
 * as in the targetForeignKeysToSourceKeys map.
 */
@Override
protected Object extractBatchKeyFromRow(AbstractRecord row, AbstractSession session) {
    int size = this.sourceKeyFields.size();
    Object[] key = new Object[size];
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    for (int index = 0; index < size; index++) {
        DatabaseField sourceField = this.sourceKeyFields.get(index);
        Object value = row.get(sourceField);
        // Must ensure the classification to get a cache hit.
        try {
            value = conversionManager.convertObject(value, sourceField.getType());
        } catch (ConversionException exception) {
            throw ConversionException.couldNotBeConverted(this, this.descriptor, exception);
        }
        key[index] = value;
    }
    return new CacheId(key);
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) 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