Search in sources :

Example 21 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method buildObjectsFromResultSetInto.

/**
 * Version of buildObjectsInto method that takes call instead of rows.
 * Return a container which contains the instances of the receivers javaClass.
 * Set the fields of the instance to the values stored in the result set.
 */
public Object buildObjectsFromResultSetInto(ReadAllQuery query, ResultSet resultSet, Vector fields, DatabaseField[] fieldsArray, Object domainObjects) throws SQLException {
    AbstractSession session = query.getSession();
    session.startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    try {
        boolean hasNext = resultSet.next();
        if (hasNext) {
            InheritancePolicy inheritancePolicy = null;
            if (this.descriptor.hasInheritance()) {
                inheritancePolicy = this.descriptor.getInheritancePolicy();
            }
            boolean isUnitOfWork = session.isUnitOfWork();
            boolean shouldCacheQueryResults = query.shouldCacheQueryResults();
            boolean shouldUseWrapperPolicy = query.shouldUseWrapperPolicy();
            // PERF: Avoid lazy init of join manager if no joining.
            JoinedAttributeManager joinManager = null;
            if (query.hasJoining()) {
                joinManager = query.getJoinedAttributeManager();
            }
            ContainerPolicy policy = query.getContainerPolicy();
            // !cp.shouldAddAll() - query with SortedListContainerPolicy - currently does not use this method
            boolean quickAdd = (domainObjects instanceof Collection) && !this.hasWrapperPolicy;
            ResultSetMetaData metaData = resultSet.getMetaData();
            ResultSetRecord row = null;
            AbstractSession executionSession = query.getExecutionSession();
            DatabaseAccessor dbAccessor = (DatabaseAccessor) query.getAccessor();
            DatabasePlatform platform = dbAccessor.getPlatform();
            boolean optimizeData = platform.shouldOptimizeDataConversion();
            if (this.isSimple) {
                // None of the fields are relational - the row could be reused, just clear all the values.
                row = new SimpleResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
                if (this.descriptor.isDescriptorTypeAggregate()) {
                    // Aggregate Collection may have an unmapped primary key referencing the owner, the corresponding field will not be used when the object is populated and therefore may not be cleared.
                    ((SimpleResultSetRecord) row).setShouldKeepValues(true);
                }
            }
            while (hasNext) {
                if (!this.isSimple) {
                    row = new ResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
                }
                Object domainObject = buildObject(query, row, joinManager, session, this.descriptor, inheritancePolicy, isUnitOfWork, shouldCacheQueryResults, shouldUseWrapperPolicy);
                if (quickAdd) {
                    ((Collection) domainObjects).add(domainObject);
                } else {
                    // query with MappedKeyMapPolicy currently does not use this method
                    policy.addInto(domainObject, domainObjects, session);
                }
                if (this.isSimple) {
                    ((SimpleResultSetRecord) row).reset();
                } else {
                    if (this.shouldKeepRow) {
                        if (row.hasResultSet()) {
                            // ResultSet has not been fully triggered - that means the cached object was used.
                            // Yet the row still may be cached in a value holder (see loadBatchReadAttributes and loadJoinedAttributes methods).
                            // Remove ResultSet to avoid attempt to trigger it (already closed) when pk or fk values (already extracted) accessed when the value holder is instantiated.
                            row.removeResultSet();
                        } else {
                            row.removeNonIndirectionValues();
                        }
                    }
                }
                hasNext = resultSet.next();
            }
        }
    } finally {
        session.endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    }
    return domainObjects;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) JoinedAttributeManager(org.eclipse.persistence.internal.queries.JoinedAttributeManager) Collection(java.util.Collection) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 22 with InheritancePolicy

use of org.eclipse.persistence.descriptors.InheritancePolicy 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)

Aggregations

InheritancePolicy (org.eclipse.persistence.descriptors.InheritancePolicy)22 Collection (java.util.Collection)7 EntityExistsException (jakarta.persistence.EntityExistsException)6 EntityManager (jakarta.persistence.EntityManager)6 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)6 OptimisticLockException (jakarta.persistence.OptimisticLockException)6 PersistenceException (jakarta.persistence.PersistenceException)6 RollbackException (jakarta.persistence.RollbackException)6 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)6 ArrayList (java.util.ArrayList)6 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)6 QueryException (org.eclipse.persistence.exceptions.QueryException)6 ValidationException (org.eclipse.persistence.exceptions.ValidationException)6 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)4 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)4 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)4 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)4 TestProblemException (org.eclipse.persistence.testing.framework.TestProblemException)4