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;
}
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);
}
}
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);
}
}
}
}
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);
}
}
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;
}
Aggregations