Search in sources :

Example 76 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method buildObjectInternal.

/**
 * Return an instance of the receivers javaClass. Set the attributes of an instance
 * from the values stored in the database row.
 */
private Object buildObjectInternal(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager, AbstractSession session, ClassDescriptor concreteDescriptor, InheritancePolicy inheritancePolicy, boolean isUnitOfWork, boolean shouldCacheQueryResults, boolean shouldUseWrapperPolicy) {
    Object domainObject = null;
    CacheKey prefechedCacheKey = null;
    Object primaryKey = extractPrimaryKeyFromRow(databaseRow, session);
    // Check for null primary key, this is not allowed.
    if ((primaryKey == null) && (!query.hasPartialAttributeExpressions()) && (!this.descriptor.isAggregateCollectionDescriptor())) {
        // property is set in the query. (As opposed to throwing an Exception).
        if (query.shouldBuildNullForNullPk()) {
            return null;
        } else {
            throw QueryException.nullPrimaryKeyInBuildingObject(query, databaseRow);
        }
    }
    if (query.getPrefetchedCacheKeys() != null) {
        prefechedCacheKey = query.getPrefetchedCacheKeys().get(primaryKey);
    }
    if ((inheritancePolicy != null) && inheritancePolicy.shouldReadSubclasses()) {
        Class<?> classValue = inheritancePolicy.classFromRow(databaseRow, session);
        concreteDescriptor = inheritancePolicy.getDescriptor(classValue);
        if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) {
            concreteDescriptor = this.descriptor;
        }
        if (concreteDescriptor == null) {
            throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue);
        }
    }
    if (isUnitOfWork) {
        // Do not wrap yet if in UnitOfWork, as there is still much more
        // processing ahead.
        domainObject = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl) session, primaryKey, prefechedCacheKey, concreteDescriptor);
    } else {
        domainObject = buildObject(false, query, databaseRow, session, primaryKey, prefechedCacheKey, concreteDescriptor, joinManager);
        if (shouldCacheQueryResults) {
            query.cacheResult(domainObject);
        }
        // wrap the object if the query requires it.
        if (shouldUseWrapperPolicy) {
            domainObject = concreteDescriptor.getObjectBuilder().wrapObject(domainObject, session);
        }
    }
    return domainObject;
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey)

Example 77 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class DatabaseQueryMechanism method shallowInsertObjectForWrite.

/**
 * Shallow insert the specified object, if necessary.
 */
protected void shallowInsertObjectForWrite(Object object, WriteObjectQuery writeQuery, CommitManager commitManager) throws DatabaseException, OptimisticLockException {
    boolean doesExist;
    if (getSession().isUnitOfWork()) {
        UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession();
        doesExist = !uow.isCloneNewObject(object);
        if (doesExist) {
            doesExist = uow.isObjectRegistered(object);
        }
    } else {
        // clone and initialize the does exist query
        DoesExistQuery existQuery = (DoesExistQuery) getDescriptor().getQueryManager().getDoesExistQuery().clone();
        existQuery.setObject(object);
        existQuery.setPrimaryKey(writeQuery.getPrimaryKey());
        existQuery.setDescriptor(getDescriptor());
        existQuery.setTranslationRow(getTranslationRow());
        doesExist = (Boolean) getSession().executeQuery(existQuery);
    }
    if (!doesExist) {
        // a shallow insert must be performed
        writeQuery.dontCascadeParts();
        insertObjectForWrite();
        // mark this object as shallow committed so that the insert will do an update
        commitManager.markShallowCommit(object);
    }
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery)

Example 78 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class NestedTableMapping method preInsert.

/**
 * INTERNAL:
 * Insert privately owned parts
 */
@Override
public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    // insert each object one by one
    ContainerPolicy cp = getContainerPolicy();
    for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
        Object object = cp.next(iter, query.getSession());
        if (isPrivateOwned()) {
            InsertObjectQuery insertQuery = new InsertObjectQuery();
            insertQuery.setIsExecutionClone(true);
            insertQuery.setObject(object);
            insertQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(insertQuery);
        } else {
            // Avoid cycles by checking commit manager, this is allowed because there is no dependency.
            if (!query.getSession().getCommitManager().isCommitInPreModify(object)) {
                ObjectChangeSet changeSet = null;
                UnitOfWorkChangeSet uowChangeSet = null;
                if (query.getSession().isUnitOfWork() && (((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet() != null)) {
                    uowChangeSet = (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
                    changeSet = (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object);
                }
                WriteObjectQuery writeQuery = new WriteObjectQuery();
                writeQuery.setIsExecutionClone(true);
                writeQuery.setObject(object);
                writeQuery.setObjectChangeSet(changeSet);
                writeQuery.setCascadePolicy(query.getCascadePolicy());
                query.getSession().executeQuery(writeQuery);
            }
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) InsertObjectQuery(org.eclipse.persistence.queries.InsertObjectQuery)

Example 79 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class ReferenceMapping method preUpdate.

/**
 * INTERNAL:
 * Update privately owned parts
 */
@Override
public void preUpdate(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!isAttributeValueInstantiated(query.getObject())) {
        return;
    }
    if (isPrivateOwned()) {
        Object objectInDatabase = readPrivateOwnedForObject(query);
        if (objectInDatabase != null) {
            query.setProperty(this, objectInDatabase);
        }
    }
    if (!shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    // Get the privately owned parts in the memory
    Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession());
    if (object != null) {
        ObjectChangeSet changeSet = null;
        UnitOfWorkChangeSet uowChangeSet = null;
        if (query.getSession().isUnitOfWork() && (((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet() != null)) {
            uowChangeSet = (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
            changeSet = (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object);
        }
        WriteObjectQuery writeQuery = new WriteObjectQuery();
        writeQuery.setIsExecutionClone(true);
        writeQuery.setObject(object);
        writeQuery.setObjectChangeSet(changeSet);
        writeQuery.setCascadePolicy(query.getCascadePolicy());
        query.getSession().executeQuery(writeQuery);
    }
}
Also used : WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)

Example 80 with UnitOfWorkImpl

use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.

the class ObjectLevelReadQuery method executeDatabaseQuery.

/**
 * INTERNAL:
 * Executes the prepared query on the datastore.
 */
@Override
public Object executeDatabaseQuery() throws DatabaseException {
    // directly from the database result-set then follow an optimized path.
    if (this.isResultSetOptimizedQuery) {
        return executeObjectLevelReadQueryFromResultSet();
    }
    if (this.session.isUnitOfWork()) {
        UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.session;
        // transaction early on the parent also.
        if (isLockQuery()) {
            if ((!unitOfWork.getCommitManager().isActive()) && (!unitOfWork.wasTransactionBegunPrematurely())) {
                unitOfWork.beginTransaction();
                unitOfWork.setWasTransactionBegunPrematurely(true);
            }
        }
        if (unitOfWork.isNestedUnitOfWork()) {
            UnitOfWorkImpl nestedUnitOfWork = (UnitOfWorkImpl) this.session;
            setSession(nestedUnitOfWork.getParent());
            Object result = executeDatabaseQuery();
            setSession(nestedUnitOfWork);
            return registerResultInUnitOfWork(result, nestedUnitOfWork, getTranslationRow(), false);
        }
    }
    // this will update the query with any settings
    this.session.validateQuery(this);
    if (this.queryId == 0) {
        this.queryId = this.session.getNextQueryId();
    }
    Object result = executeObjectLevelReadQuery();
    if (result != null) {
        if (getLoadGroup() != null) {
            Object resultToLoad = result;
            if (this.shouldIncludeData) {
                resultToLoad = ((ComplexQueryResult) result).getResult();
            }
            session.load(resultToLoad, getLoadGroup(), getDescriptor(), false);
        } else {
            FetchGroup executionFetchGroup = getExecutionFetchGroup();
            if (executionFetchGroup != null) {
                LoadGroup lg = executionFetchGroup.toLoadGroupLoadOnly();
                if (lg != null) {
                    Object resultToLoad = result;
                    if (this.shouldIncludeData) {
                        resultToLoad = ((ComplexQueryResult) result).getResult();
                    }
                    session.load(resultToLoad, lg, getDescriptor(), true);
                }
            }
        }
    }
    return result;
}
Also used : UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject)

Aggregations

UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)92 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)21 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)19 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)18 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)15 EntityManager (jakarta.persistence.EntityManager)14 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)11 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)9 Vector (java.util.Vector)8 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)8 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)8 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)7 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)7 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)7 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)6 UnitOfWorkChangeSet (org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet)6 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)5 BigDecimal (java.math.BigDecimal)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)5