Program: 63548
Project: 63548
Buggy source:
/** 
 * Perform a cache lookup for the query This is only called from read object query. The query has already checked that the cache should be checked.
 */
@Override public Object checkCacheForObject(AbstractRecord translationRow,AbstractSession session){
  ReadObjectQuery query=getReadObjectQuery();
  ClassDescriptor descriptor=getDescriptor();
  boolean conforming=false;
  UnitOfWorkImpl uow=null;
  if (session.isUnitOfWork()) {
    conforming=query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork();
    uow=(UnitOfWorkImpl)session;
  }
  int policyToUse=query.getInMemoryQueryIndirectionPolicyState();
  if (conforming && (policyToUse != InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION)) {
    policyToUse=InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_CONFORMED;
  }
  Object cachedObject=null;
  Expression selectionCriteria=getSelectionCriteria();
  Object selectionKey=query.getSelectionId();
  Object selectionObject=query.getSelectionObject();
  if ((selectionKey != null) || (selectionObject != null)) {
    if (selectionKey == null) {
      selectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromObject(selectionObject,session,true);
      if (selectionKey == null) {
        return InvalidObject.instance;
      }
      query.setSelectionId(selectionKey);
    }
    if (query.requiresDeferredLocks()) {
      cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
    }
 else {
      cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
    }
  }
 else {
    if (translationRow != null && translationRow.hasSopObject()) {
      if (query.requiresDeferredLocks()) {
        cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(translationRow.getSopObject(),session),query.getReferenceClass(),false,descriptor);
      }
 else {
        cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(translationRow.getSopObject(),session),query.getReferenceClass(),false,descriptor);
      }
    }
 else {
      if (selectionCriteria == null) {
        if (query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork() || query.shouldCheckCacheOnly()|| query.shouldCheckCacheThenDatabase()) {
          cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(null,query.getReferenceClass(),translationRow,policyToUse,conforming,false,descriptor);
        }
      }
 else {
        selectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(true,selectionCriteria,translationRow,session);
        if ((selectionKey != null) || query.shouldCheckCacheByExactPrimaryKey()) {
          if (selectionKey != null) {
            if (selectionKey == InvalidObject.instance) {
              return selectionKey;
            }
            if (query.requiresDeferredLocks()) {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
            }
 else {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
            }
          }
        }
 else {
          Object inexactSelectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(false,selectionCriteria,translationRow,session);
          if (inexactSelectionKey != null) {
            if (selectionKey == InvalidObject.instance) {
              return selectionKey;
            }
            if (query.requiresDeferredLocks()) {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
            }
 else {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
            }
          }
 else {
            CacheKey cacheKey=descriptor.getCachePolicy().checkCacheByIndex(selectionCriteria,translationRow,descriptor,session);
            if (cacheKey != null) {
              if (query.requiresDeferredLocks()) {
                cacheKey.checkDeferredLock();
              }
 else {
                cacheKey.checkReadLock();
              }
              cachedObject=cacheKey.getObject();
            }
          }
          if (cachedObject != null) {
            try {
              ExpressionBuilder builder=selectionCriteria.getBuilder();
              builder.setSession(session.getRootSession(null));
              builder.setQueryClass(descriptor.getJavaClass());
              if (!selectionCriteria.doesConform(cachedObject,session,translationRow,policyToUse)) {
                cachedObject=null;
              }
            }
 catch (            QueryException exception) {
              if (query.shouldCheckCacheOnly()) {
                throw exception;
              }
              cachedObject=null;
            }
          }
          boolean conformingButOutsideUnitOfWork=((query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork()) && !session.isUnitOfWork());
          if ((cachedObject == null) && (conforming || (!query.shouldCheckCacheByPrimaryKey() && !conformingButOutsideUnitOfWork))) {
            if (selectionCriteria != null) {
              ExpressionBuilder builder=selectionCriteria.getBuilder();
              builder.setSession(session.getRootSession(null));
              builder.setQueryClass(descriptor.getJavaClass());
            }
            try {
              cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionCriteria,query.getReferenceClass(),translationRow,policyToUse,conforming,false,descriptor);
            }
 catch (            QueryException exception) {
              if (query.shouldCheckCacheOnly()) {
                throw exception;
              }
            }
          }
        }
      }
    }
  }
  if (conforming) {
    if (cachedObject == null) {
      if (selectionKey != null) {
        cachedObject=uow.getObjectFromNewObjects(query.getReferenceClass(),selectionKey);
      }
 else {
        if (selectionCriteria != null) {
          ExpressionBuilder builder=selectionCriteria.getBuilder();
          builder.setSession(session.getRootSession(null));
          builder.setQueryClass(descriptor.getJavaClass());
        }
        try {
          cachedObject=uow.getObjectFromNewObjects(selectionCriteria,query.getReferenceClass(),translationRow,policyToUse);
        }
 catch (        QueryException exception) {
        }
      }
    }
    if (cachedObject != null) {
      if (uow.isObjectDeleted(cachedObject)) {
        if (selectionKey != null) {
          return InvalidObject.instance;
        }
 else {
          cachedObject=null;
        }
      }
    }
  }
  if (cachedObject != null) {
    if (descriptor.hasFetchGroupManager()) {
      if (descriptor.getFetchGroupManager().isPartialObject(cachedObject)) {
        FetchGroup fetchGroup=query.getExecutionFetchGroup(descriptor);
        EntityFetchGroup entityFetchGroup=null;
        if (fetchGroup != null) {
          entityFetchGroup=descriptor.getFetchGroupManager().getEntityFetchGroup(fetchGroup);
        }
        if (!descriptor.getFetchGroupManager().isObjectValidForFetchGroup(cachedObject,entityFetchGroup)) {
          cachedObject=null;
        }
      }
    }
  }
  if ((cachedObject == null) && query.shouldCheckCacheOnly() && ((uow == null) || (!uow.isNestedUnitOfWork() && descriptor.getCachePolicy().shouldIsolateObjectsInUnitOfWork()))) {
    return InvalidObject.instance;
  }
  return cachedObject;
}

Fixed version (ground truth):
/** 
 * Perform a cache lookup for the query This is only called from read object query. The query has already checked that the cache should be checked.
 */
@Override public Object checkCacheForObject(AbstractRecord translationRow,AbstractSession session){
  ReadObjectQuery query=getReadObjectQuery();
  ClassDescriptor descriptor=getDescriptor();
  boolean conforming=false;
  UnitOfWorkImpl uow=null;
  if (session.isUnitOfWork()) {
    conforming=query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork();
    uow=(UnitOfWorkImpl)session;
  }
  int policyToUse=query.getInMemoryQueryIndirectionPolicyState();
  if (conforming && (policyToUse != InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION)) {
    policyToUse=InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_NOT_CONFORMED;
  }
  Object cachedObject=null;
  Expression selectionCriteria=getSelectionCriteria();
  Object selectionKey=query.getSelectionId();
  Object selectionObject=query.getSelectionObject();
  if ((selectionKey != null) || (selectionObject != null)) {
    if (selectionKey == null) {
      selectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromObject(selectionObject,session,true);
      if (selectionKey == null) {
        return InvalidObject.instance;
      }
      query.setSelectionId(selectionKey);
    }
    if (query.requiresDeferredLocks()) {
      cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
    }
 else {
      cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
    }
  }
 else {
    if (translationRow != null && translationRow.hasSopObject()) {
      if (query.requiresDeferredLocks()) {
        cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(translationRow.getSopObject(),session),query.getReferenceClass(),false,descriptor);
      }
 else {
        cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(descriptor.getObjectBuilder().extractPrimaryKeyFromObject(translationRow.getSopObject(),session),query.getReferenceClass(),false,descriptor);
      }
    }
 else {
      if (selectionCriteria == null) {
        if (query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork() || query.shouldCheckCacheOnly()|| query.shouldCheckCacheThenDatabase()) {
          cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(null,query.getReferenceClass(),translationRow,policyToUse,conforming,false,descriptor);
        }
      }
 else {
        selectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(true,selectionCriteria,translationRow,session);
        if ((selectionKey != null) || query.shouldCheckCacheByExactPrimaryKey()) {
          if (selectionKey != null) {
            if (selectionKey == InvalidObject.instance) {
              return selectionKey;
            }
            if (query.requiresDeferredLocks()) {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
            }
 else {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
            }
          }
        }
 else {
          Object inexactSelectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(false,selectionCriteria,translationRow,session);
          if (inexactSelectionKey != null) {
            if (selectionKey == InvalidObject.instance) {
              return selectionKey;
            }
            if (query.requiresDeferredLocks()) {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMapWithDeferredLock(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
            }
 else {
              cachedObject=session.getIdentityMapAccessorInstance().getFromLocalIdentityMap(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
            }
          }
 else {
            CacheKey cacheKey=descriptor.getCachePolicy().checkCacheByIndex(selectionCriteria,translationRow,descriptor,session);
            if (cacheKey != null) {
              if (query.requiresDeferredLocks()) {
                cacheKey.checkDeferredLock();
              }
 else {
                cacheKey.checkReadLock();
              }
              cachedObject=cacheKey.getObject();
            }
          }
          if (cachedObject != null) {
            try {
              ExpressionBuilder builder=selectionCriteria.getBuilder();
              builder.setSession(session.getRootSession(null));
              builder.setQueryClass(descriptor.getJavaClass());
              if (!selectionCriteria.doesConform(cachedObject,session,translationRow,policyToUse)) {
                cachedObject=null;
              }
            }
 catch (            QueryException exception) {
              if (query.shouldCheckCacheOnly()) {
                throw exception;
              }
              cachedObject=null;
            }
          }
          boolean conformingButOutsideUnitOfWork=((query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork()) && !session.isUnitOfWork());
          if ((cachedObject == null) && (conforming || (!query.shouldCheckCacheByPrimaryKey() && !conformingButOutsideUnitOfWork))) {
            if (selectionCriteria != null) {
              ExpressionBuilder builder=selectionCriteria.getBuilder();
              builder.setSession(session.getRootSession(null));
              builder.setQueryClass(descriptor.getJavaClass());
            }
            try {
              cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionCriteria,query.getReferenceClass(),translationRow,policyToUse,conforming,false,descriptor);
            }
 catch (            QueryException exception) {
              if (query.shouldCheckCacheOnly()) {
                throw exception;
              }
            }
          }
        }
      }
    }
  }
  if (conforming) {
    if (cachedObject == null) {
      if (selectionKey != null) {
        cachedObject=uow.getObjectFromNewObjects(query.getReferenceClass(),selectionKey);
      }
 else {
        if (selectionCriteria != null) {
          ExpressionBuilder builder=selectionCriteria.getBuilder();
          builder.setSession(session.getRootSession(null));
          builder.setQueryClass(descriptor.getJavaClass());
        }
        try {
          cachedObject=uow.getObjectFromNewObjects(selectionCriteria,query.getReferenceClass(),translationRow,policyToUse);
        }
 catch (        QueryException exception) {
        }
      }
    }
    if (cachedObject != null) {
      if (uow.isObjectDeleted(cachedObject)) {
        if (selectionKey != null) {
          return InvalidObject.instance;
        }
 else {
          cachedObject=null;
        }
      }
    }
  }
  if (cachedObject != null) {
    if (descriptor.hasFetchGroupManager()) {
      if (descriptor.getFetchGroupManager().isPartialObject(cachedObject)) {
        FetchGroup fetchGroup=query.getExecutionFetchGroup(descriptor);
        EntityFetchGroup entityFetchGroup=null;
        if (fetchGroup != null) {
          entityFetchGroup=descriptor.getFetchGroupManager().getEntityFetchGroup(fetchGroup);
        }
        if (!descriptor.getFetchGroupManager().isObjectValidForFetchGroup(cachedObject,entityFetchGroup)) {
          cachedObject=null;
        }
      }
    }
  }
  if ((cachedObject == null) && query.shouldCheckCacheOnly() && ((uow == null) || (!uow.isNestedUnitOfWork() && descriptor.getCachePolicy().shouldIsolateObjectsInUnitOfWork()))) {
    return InvalidObject.instance;
  }
  return cachedObject;
}