query.setSession(session); if (session.isUnitOfWork()) { original = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl)session, primaryKey, concreteDescriptor); } else { original = buildObject(query, databaseRow, session, primaryKey, concreteDescriptor, joinManager); if (query.shouldCacheQueryResults()) { query.cacheResult(original); query.setSession(unitOfWork); clone = query.registerIndividualResult(original, unitOfWork, joinManager); } else { clone = query.registerIndividualResult(original, unitOfWork, null);
/** * INTERNAL: * Check to see if this query needs to be prepare and prepare it. * The prepare is done on the original query to ensure that the work is not repeated. */ public void checkPrepare(AbstractSession session, AbstractRecord translationRow, boolean force) { // CR#3823735 For custom queries the prePrepare may not have been called yet. checkPrePrepare(session); super.checkPrepare(session, translationRow, force); }
/** * INTERNAL: * Clone the query, including its selection criteria. * <p> * Normally selection criteria are not cloned here as they are cloned * later on during prepare. */ public Object deepClone() { return clone(); }
if (!query.hasPartialAttributeExpressions()) { for (Enumeration enumeration = getFields().elements(); enumeration.hasMoreElements();) { DatabaseField field = (DatabaseField)enumeration.nextElement(); if (targetQuery.isObjectLevelReadQuery() && (query.shouldCascadeAllParts() || (query.shouldCascadePrivateParts() && isPrivateOwned()) || (query.shouldCascadeByMapping() && this.cascadeRefresh))) { targetQuery = (ObjectLevelReadQuery)targetQuery.clone(); ((ObjectLevelReadQuery)targetQuery).setShouldRefreshIdentityMapResult(query.shouldRefreshIdentityMapResult()); targetQuery.setCascadePolicy(query.getCascadePolicy()); targetQuery.setQueryId(query.getQueryId()); targetQuery.setShouldMaintainCache(query.shouldMaintainCache()); return getIndirectionPolicy().valueFromQuery(targetQuery, row, query.getSession());
if (query.shouldMaintainCache() && (!query.shouldRetrieveBypassCache() || !query.shouldStoreBypassCache())) { cacheKey = session.retrieveCacheKey(primaryKey, concreteDescriptor, joinManager, query); protectedObject = cacheKey.getObject(); FetchGroup fetchGroup = query.getExecutionFetchGroup(concreteDescriptor); FetchGroupManager fetchGroupManager = concreteDescriptor.getFetchGroupManager(); if (protectedObject == null || query.shouldRetrieveBypassCache()) { cacheHit = false; boolean domainWasMissing = protectedObject == null; if (protectedObject == null || query.shouldStoreBypassCache()){ if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) { protectedObject = ((ReadObjectQuery)query).getSelectionObject(); } else { if (query.shouldMaintainCache() && ! query.shouldStoreBypassCache()){ if (domainWasMissing) { // may have build a new domain even though there is one in the cache cacheKey.setObject(protectedObject); }else if (cacheKey == null || (domainWasMissing && query.shouldRetrieveBypassCache())){ cacheKey = new CacheKey(primaryKey); cacheKey.setObject(protectedObject); if (query.shouldMaintainCache() && ! query.shouldStoreBypassCache()) { if (preFetchedCacheKey == null){ sharedCacheKey = session.getParent().retrieveCacheKey(primaryKey, concreteDescriptor, joinManager, query); if (query.shouldMaintainCache() && ! query.shouldStoreBypassCache()) {
if (sourceQuery.isReadAllQuery() && (((ReadAllQuery)sourceQuery).isAttributeBatchRead(getDescriptor(), getAttributeName()) || shouldUseBatchReading())) { return batchedValueFromRow(row, ((ReadAllQuery)sourceQuery)); if (sourceQuery.isObjectLevelReadQuery() && (sourceQuery.shouldCascadeAllParts() || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned()) || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh)) ) { query.setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult()); query.setCascadePolicy(sourceQuery.getCascadePolicy()); query.setShouldMaintainCache(sourceQuery.shouldMaintainCache()); query.setQueryId(sourceQuery.getQueryId());
FetchGroup sourceFG = sourceQuery.getExecutionFetchGroup(this.getDescriptor()); if (sourceFG != null) { FetchGroup targetFetchGroup = sourceFG.getGroup(getAttributeName()); targetQuery.setIsExecutionClone(true); targetQuery.setQueryId(sourceQuery.getQueryId()); if (sourceQuery.usesResultSetAccessOptimization()) { targetQuery.setAccessors(sourceQuery.getAccessors()); ((ObjectLevelReadQuery)targetQuery).setRequiresDeferredLocks(sourceQuery.requiresDeferredLocks()); if (sourceQuery.shouldCascadeAllParts() || (this.isPrivateOwned && sourceQuery.shouldCascadePrivateParts()) || (this.cascadeRefresh && sourceQuery.shouldCascadeByMapping())) { ((ObjectLevelReadQuery)targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult()); targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy()); targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache()); ReadQuery dedicatedQuery = getExtendPessimisticLockScopeDedicatedQuery(executionSession, sourceQuery.getLockMode()); executionSession.executeQuery(dedicatedQuery, row);
CacheKey unitOfWorkCacheKey = unitOfWork.getIdentityMapAccessorInstance().acquireLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor, query.isCacheCheckComplete()); Object clone = unitOfWorkCacheKey.getObject(); boolean found = clone != null; if (!found || query.shouldRefreshIdentityMapResult() || query.shouldCacheQueryResults() // Need to build original to cache it. || query.shouldRetrieveBypassCache() || (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(clone))) { query.setSession(session); if (session.isUnitOfWork()) { return query.registerIndividualResult(original, primaryKey, unitOfWork, joinManager, concreteDescriptor); } else { return query.registerIndividualResult(original, primaryKey, unitOfWork, null, concreteDescriptor); if (query.shouldCacheQueryResults()) { query.cacheResult(original); query.recordCloneForPessimisticLocking(original, unitOfWork); return original; if (!query.isRegisteringResults()) { return original; query.postRegisterIndividualResult(clone, original, primaryKey, unitOfWork, joinManager, concreteDescriptor); } finally { unitOfWorkCacheKey.release(); query.setSession(unitOfWork);
FetchGroup fetchGroup = query.getExecutionFetchGroup(descriptor); FetchGroupManager fetchGroupManager = descriptor.getFetchGroupManager(); try { boolean isARefresh = query.shouldRefreshIdentityMapResult() || (query.isLockQuery() && (!wasAClone || !query.isClonePessimisticLocked(workingClone, unitOfWork))); boolean isUnionFetchGroups = false; if ((!isARefresh) && (!isIsolated) && !query.shouldRetrieveBypassCache() && !unitOfWork.shouldReadFromDB() && (!unitOfWork.shouldForceReadFromDB(query, primaryKey))) { AbstractSession session = unitOfWork.getParentIdentityMapSession(query); if (preFetchedCacheKey == null){ isARefresh = wasAnOriginal && (descriptor.shouldAlwaysRefreshCache() || descriptor.getCacheInvalidationPolicy().isInvalidated(originalCacheKey, query.getExecutionTime())); if (wasAnOriginal && !query.shouldRetrieveBypassCache()) { workingClone = instantiateWorkingCopyClone(original, unitOfWork); if (descriptor.hasSerializedObjectPolicy() && query.shouldUseSerializedObjectPolicy() && !databaseRow.hasSopObject()) { if (wasAClone && (unitOfWorkCacheKey.getLastUpdatedQueryId() >= query.getQueryId())) { return workingClone; if (isARefresh && fetchGroupManager != null) { if (isUnionFetchGroups && fetchGroupManager.isPartialObject(workingClone)) { fetchGroupManager.unionEntityFetchGroupIntoObject(workingClone, fetchGroupManager.getEntityFetchGroup(query.getExecutionFetchGroup(this.descriptor)), unitOfWork, false); } else { fetchGroupManager.setObjectFetchGroup(workingClone, query.getExecutionFetchGroup(this.descriptor), unitOfWork); if (!unitOfWork.wasTransactionBegunPrematurely() && descriptor.getCachePolicy().isProtectedIsolation() && !isIsolated && !query.shouldStoreBypassCache()) {
if (sourceQuery.isReadAllQuery() && (((ReadAllQuery)sourceQuery).isAttributeBatchRead(getDescriptor(), getAttributeName()) || shouldUseBatchReading())) { return batchedValueFromRow(row, (ReadAllQuery)sourceQuery); if ((getHistoryPolicy() != null) || (sourceQuery.getSession().getAsOfClause() != null) || ((sourceQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)sourceQuery).hasAsOfClause()) && (sourceQuery.shouldCascadeAllParts() || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned()) || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) || extendingPessimisticLockScope) { targetQuery = (ReadQuery)targetQuery.clone(); if(sourceQuery.isObjectLevelReadQuery()) { statement.getBuilder().asOf(((ObjectLevelReadQuery)sourceQuery).getAsOfClause()); statement.setLockingClause(new ForUpdateClause(sourceQuery.getLockMode())); if (sourceQuery.getSession().getAsOfClause() != null) { builder.asOf(sourceQuery.getSession().getAsOfClause()); } else if (builder.getAsOfClause() == null) { builder.asOf(AsOfClause.NO_CLAUSE); statement.normalize(sourceQuery.getSession(), null); targetQuery.setSQLStatement(statement); return getIndirectionPolicy().valueFromQuery(targetQuery, row, sourceQuery.getSession());
nestedQuery.setShouldUseSerializedObjectPolicy(baseQuery.shouldUseSerializedObjectPolicy()); if (baseQuery.hasPartialAttributeExpressions()) { nestedQuery.setPartialAttributeExpressions(extractNestedExpressions(((ObjectLevelReadQuery)baseQuery).getPartialAttributeExpressions(), nestedQuery.getExpressionBuilder())); FetchGroup sourceFG = baseQuery.getExecutionFetchGroup(); if (sourceFG != null) { FetchGroup targetFetchGroup = sourceFG.getGroup(getAttributeName()); if (baseQuery.isLockQuery()) { if (((ObjectLevelReadQuery)baseQuery).getLockingClause().isForUpdateOfClause()) { ForUpdateOfClause clause = (ForUpdateOfClause)((ObjectLevelReadQuery)baseQuery).getLockingClause().clone(); nestedQuery.setShouldMaintainCache(baseQuery.shouldMaintainCache()); nestedQuery.setShouldRefreshIdentityMapResult(baseQuery.shouldRefreshIdentityMapResult()); if (baseQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)baseQuery).hasAsOfClause()) { nestedQuery.setAsOfClause(((ObjectLevelReadQuery)baseQuery).getAsOfClause()); nestedQuery.setCascadePolicy(baseQuery.getCascadePolicy()); if (nestedQuery.hasJoining()) { nestedQuery.getJoinedAttributeManager().computeJoiningMappingQueries(session); nestedQuery.setRequiresDeferredLocks(baseQuery.requiresDeferredLocks());
if (query.shouldMaintainCache()) { if (domainObject == null || query.shouldRetrieveBypassCache()) { cacheHit = false; if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) { domainObject = ((ReadObjectQuery)query).getSelectionObject(); } else { if (query.shouldMaintainCache() && ! query.shouldStoreBypassCache()) { cacheKey.setObject(domainObject); copyQueryInfoToCacheKey(cacheKey, query, databaseRow, session, concreteDescriptor); if (query.shouldMaintainCache() && ! query.shouldStoreBypassCache()) { if ((query.getFetchGroup() != null) && concreteDescriptor.hasFetchGroupManager()) { concreteDescriptor.getFetchGroupManager().setObjectFetchGroup(domainObject, query.getFetchGroup(), session); if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) { copyInto(domainObject, ((ReadObjectQuery)query).getSelectionObject()); domainObject = ((ReadObjectQuery)query).getSelectionObject(); boolean isInvalidated = concreteDescriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey, query.getExecutionTime()); if ((cacheKey.getMutex().getActiveThread() == Thread.currentThread()) && ((query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache() || isInvalidated) && ((cacheKey.getLastUpdatedQueryId() != query.getQueryId()) && !cacheKey.getMutex().isLockedByMergeManager()))) { refreshRequired = policy.isNewerVersion(databaseRow, domainObject, primaryKey, session); if (!refreshRequired) { cacheKey.setReadTime(query.getExecutionTime());
boolean isARefresh = query.shouldRefreshIdentityMapResult() || (query.isLockQuery() && (!wasAClone || !query.isClonePessimisticLocked(workingClone, unitOfWork))); if (wasAClone && descriptor.hasFetchGroupManager() && (descriptor.getFetchGroupManager().isPartialObject(workingClone) && (!descriptor.getFetchGroupManager().isObjectValidForFetchGroup(workingClone, query.getFetchGroup())))) { isARefresh = true; wasAnOriginal = original != null; isARefresh = wasAnOriginal && (descriptor.shouldAlwaysRefreshCache() || descriptor.getCacheInvalidationPolicy().isInvalidated(originalCacheKey, query.getExecutionTime())); if (wasAnOriginal && (!isARefresh) && ! query.shouldRetrieveBypassCache()) { return unitOfWork.cloneAndRegisterObject(original, originalCacheKey, unitOfWorkCacheKey, descriptor); if (wasAnOriginal && query.shouldStoreBypassCache()) { workingClone = instantiateWorkingCopyClone(original, unitOfWork); if (wasAClone && (unitOfWorkCacheKey.getLastUpdatedQueryId() >= query.getQueryId())) { return workingClone; this.descriptor.getFetchGroupManager().setObjectFetchGroup(workingClone, query.getFetchGroup(), unitOfWork); this.descriptor.getFetchGroupManager().setObjectFetchGroup(workingClone, query.getFetchGroup(), unitOfWork); query.recordCloneForPessimisticLocking(workingClone, unitOfWork);
AbstractSession session = query.getSession(); session.startOperationProfile(SessionProfiler.OBJECT_BUILDING, query, SessionProfiler.ALL); if ((primaryKey == null) && (!query.hasPartialAttributeExpressions()) && (!this.descriptor.isAggregateCollectionDescriptor())) { if (query.shouldBuildNullForNullPk()) { return null; } else { Class classValue = concreteDescriptor.getInheritancePolicy().classFromRow(databaseRow, session); concreteDescriptor = concreteDescriptor.getInheritancePolicy().getDescriptor(classValue); if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) { concreteDescriptor = this.descriptor; } else { domainObject = buildObject(query, databaseRow, session, primaryKey, concreteDescriptor, joinManager); if (query.shouldCacheQueryResults()) { query.cacheResult(domainObject); if (query.shouldUseWrapperPolicy()) { domainObject = concreteDescriptor.getObjectBuilder().wrapObject(domainObject, session);
nestedQuery.setSession(session); if (baseQuery.hasPartialAttributeExpressions()) { nestedQuery.setPartialAttributeExpressions(extractNestedExpressions(((ObjectLevelReadQuery)baseQuery).getPartialAttributeExpressions(), nestedQuery.getExpressionBuilder(), false)); if (baseQuery.isLockQuery()) { if (((ObjectLevelReadQuery)baseQuery).getLockingClause().isForUpdateOfClause()) { ForUpdateOfClause clause = (ForUpdateOfClause)((ObjectLevelReadQuery)baseQuery).getLockingClause().clone(); nestedQuery.setShouldMaintainCache(baseQuery.shouldMaintainCache()); nestedQuery.setShouldRefreshIdentityMapResult(baseQuery.shouldRefreshIdentityMapResult()); if (baseQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)baseQuery).hasAsOfClause()) { nestedQuery.setAsOfClause(((ObjectLevelReadQuery)baseQuery).getAsOfClause()); nestedQuery.setCascadePolicy(baseQuery.getCascadePolicy()); if (nestedQuery.hasJoining()) { nestedQuery.getJoinedAttributeManager().computeJoiningMappingQueries(session);
if (sourceQuery != null && sourceQuery.isObjectBuildingQuery() && sourceQuery.shouldRefreshIdentityMapResult()) { refreshCascade = sourceQuery.getCascadePolicy(); if (executionSession.isUnitOfWork() && sourceQuery.shouldRefreshIdentityMapResult() || databaseRow.hasSopObject()){
/** * Clean up the cached object data and only revert the fetch group data back to the cached object. */ private void revertFetchGroupData(Object domainObject, ClassDescriptor concreteDescriptor, CacheKey cacheKey, ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, AbstractSession session, boolean targetIsProtected) { FetchGroup fetchGroup = query.getExecutionFetchGroup(concreteDescriptor); FetchGroupManager fetchGroupManager = concreteDescriptor.getFetchGroupManager(); //the cached object is either invalidated, or staled as the version is newer, or a refresh is explicitly set on the query. //clean all data of the cache object. fetchGroupManager.reset(domainObject); //set fetch group reference to the cached object fetchGroupManager.setObjectFetchGroup(domainObject, fetchGroupManager.getEntityFetchGroup(fetchGroup), session); // Bug 276362 - set the CacheKey's read time (to re-validate the CacheKey) before buildAttributesIntoObject is called cacheKey.setReadTime(query.getExecutionTime()); //read in the fetch group data only concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, cacheKey, databaseRow, query, joinManager, fetchGroup, false, session); //set refresh on fetch group fetchGroupManager.setRefreshOnFetchGroupToObject(domainObject, (query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache())); //set query id to prevent infinite recursion on refresh object cascade all cacheKey.setLastUpdatedQueryId(query.getQueryId()); //register the object into the IM and set the write lock object if applied. if (concreteDescriptor.usesOptimisticLocking()) { OptimisticLockingPolicy policy = concreteDescriptor.getOptimisticLockingPolicy(); cacheKey.setWriteLockValue(policy.getValueToPutInCache(databaseRow, session)); } }
Object primaryKey = extractPrimaryKeyFromRow(databaseRow, session); if ((primaryKey == null) && (!query.hasPartialAttributeExpressions()) && (!this.descriptor.isAggregateCollectionDescriptor())) { if (query.shouldBuildNullForNullPk()) { return null; } else { if (query.getPrefetchedCacheKeys() != null){ prefechedCacheKey = query.getPrefetchedCacheKeys().get(primaryKey); if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) { concreteDescriptor = this.descriptor; domainObject = buildObject(false, query, databaseRow, session, primaryKey, prefechedCacheKey, concreteDescriptor, joinManager); if (shouldCacheQueryResults) { query.cacheResult(domainObject);
if (sourceQuery != null && sourceQuery.isObjectBuildingQuery() && sourceQuery.shouldRefreshIdentityMapResult()) { refreshCascade = sourceQuery.getCascadePolicy(); element = getConverter().convertDataValueToObjectValue(element, executionSession); cp.addInto(element, result, sourceQuery.getSession());
if (sourceQuery.isObjectLevelReadQuery()){ if (((ObjectLevelReadQuery)nestedQuery).isPartialAttribute(getAttributeName()) || ((joinManager != null) && joinManager.isAttributeJoined(getDescriptor(), getAttributeName()))) { nestedQuery = (ObjectLevelReadQuery)nestedQuery.deepClone(); ((ObjectLevelReadQuery)nestedQuery).getJoinedAttributeManager().setJoinedAttributeExpressions_(extractNestedExpressions(joinManager.getJoinedAttributeExpressions(), joinManager.getBaseExpressionBuilder(), false)); nestedQuery.setDescriptor(descriptor); if (sourceQuery.isReadAllQuery() && ((ReadAllQuery)sourceQuery).isAttributeBatchRead(getDescriptor(), getAttributeName())) { nestedQuery = (ObjectLevelReadQuery)sourceQuery.clone(); nestedQuery.setProperties(sourceQuery.getProperties());