Program: 68332
Project: 68332
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.
 */
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();
  Vector selectionKey=query.getSelectionKey();
  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.setSelectionKey(selectionKey);
    }
    if (descriptor.shouldAcquireCascadedLocks()) {
      cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
    }
 else {
      cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
    }
    if (selectionKey.contains(null)) {
      return InvalidObject.instance;
    }
  }
 else {
    if (selectionCriteria == null) {
      if (query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork() || query.shouldCheckCacheOnly()|| query.shouldCheckCacheThenDatabase()) {
        cachedObject=session.getIdentityMapAccessorInstance().getIdentityMapManager().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 (descriptor.shouldAcquireCascadedLocks()) {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
          }
 else {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
          }
          if (selectionKey.contains(null)) {
            return InvalidObject.instance;
          }
        }
      }
 else {
        Vector inexactSelectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(false,selectionCriteria,translationRow,session);
        if (inexactSelectionKey != null) {
          if (descriptor.shouldAcquireCascadedLocks()) {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
          }
 else {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
          }
          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().getIdentityMapManager().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) {
        if (!uow.shouldNewObjectsBeCached()) {
          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)) {
        if (!descriptor.getFetchGroupManager().isObjectValidForFetchGroup(cachedObject,query.getFetchGroup())) {
          cachedObject=null;
        }
      }
    }
  }
  if ((cachedObject == null) && query.shouldCheckCacheOnly()) {
    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.
 */
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();
  Vector selectionKey=query.getSelectionKey();
  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.setSelectionKey(selectionKey);
    }
    if (descriptor.shouldAcquireCascadedLocks()) {
      cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
    }
 else {
      cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
    }
    if (selectionKey.contains(null)) {
      return InvalidObject.instance;
    }
  }
 else {
    if (selectionCriteria == null) {
      if (query.shouldConformResultsInUnitOfWork() || descriptor.shouldAlwaysConformResultsInUnitOfWork() || query.shouldCheckCacheOnly()|| query.shouldCheckCacheThenDatabase()) {
        cachedObject=session.getIdentityMapAccessorInstance().getIdentityMapManager().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 (descriptor.shouldAcquireCascadedLocks()) {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(selectionKey,query.getReferenceClass(),false,descriptor);
          }
 else {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(selectionKey,query.getReferenceClass(),false,descriptor);
          }
          if (selectionKey.contains(null)) {
            return InvalidObject.instance;
          }
        }
      }
 else {
        Vector inexactSelectionKey=descriptor.getObjectBuilder().extractPrimaryKeyFromExpression(false,selectionCriteria,translationRow,session);
        if (inexactSelectionKey != null) {
          if (descriptor.shouldAcquireCascadedLocks()) {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
          }
 else {
            cachedObject=session.getIdentityMapAccessorInstance().getFromIdentityMap(inexactSelectionKey,query.getReferenceClass(),false,descriptor);
          }
          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().getIdentityMapManager().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) {
        if (!uow.shouldNewObjectsBeCached()) {
          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)) {
        if (!descriptor.getFetchGroupManager().isObjectValidForFetchGroup(cachedObject,query.getFetchGroup())) {
          cachedObject=null;
        }
      }
    }
  }
  if ((cachedObject == null) && query.shouldCheckCacheOnly() && !session.isUnitOfWork()) {
    return InvalidObject.instance;
  }
  return cachedObject;
}