Program: 63330
Project: 63330
Buggy source:
/** 
 * INTERNAL: Return the value of the reference attribute or a value holder. Check whether the mapping's attribute should be optimized through batch and joining.
 * @param shouldUseSopObject indicates whether sopObject stored in the row should be used to extract the value (and fields/values stored in the row ignored).
 */
protected Object valueFromRowInternal(AbstractRecord row,JoinedAttributeManager joinManager,ObjectBuildingQuery sourceQuery,AbstractSession executionSession,boolean shouldUseSopObject) throws DatabaseException {
  ReadQuery targetQuery=this.selectionQuery;
  if (shouldUseSopObject) {
    Object sopAttribute=getAttributeValueFromObject(row.getSopObject());
    Object sopRealAttribute;
    if (isCollectionMapping()) {
      if (sopAttribute == null) {
        return getContainerPolicy().containerInstance();
      }
      sopRealAttribute=getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(),sopAttribute);
      if (getContainerPolicy().isEmpty(sopRealAttribute)) {
        return sopAttribute;
      }
    }
 else {
      if (sopAttribute == null) {
        return this.indirectionPolicy.nullValueFromRow();
      }
      sopRealAttribute=getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(),sopAttribute);
      if (sopRealAttribute == null) {
        return sopAttribute;
      }
    }
    DatabaseRecord sopRow=new DatabaseRecord(0);
    sopRow.setSopObject(sopRealAttribute);
    row=sopRow;
  }
  if (targetQuery.isObjectLevelReadQuery() && targetQuery.getDescriptor().hasFetchGroupManager()) {
    FetchGroup sourceFG=sourceQuery.getExecutionFetchGroup(this.getDescriptor());
    if (sourceFG != null) {
      FetchGroup targetFetchGroup=sourceFG.getGroup(getAttributeName());
      if (targetFetchGroup != null) {
        if (targetQuery.shouldPrepare()) {
          targetQuery.checkPrepare(executionSession,row);
        }
        targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
        targetQuery.setIsExecutionClone(true);
        ((ObjectLevelReadQuery)targetQuery).setFetchGroup(targetFetchGroup);
      }
    }
  }
  if (!this.indirectionPolicy.usesIndirection() || shouldUseSopObject) {
    if (targetQuery == this.selectionQuery) {
      if (targetQuery.shouldPrepare()) {
        targetQuery.checkPrepare(executionSession,row);
      }
      targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
      targetQuery.setIsExecutionClone(true);
    }
    targetQuery.setQueryId(sourceQuery.getQueryId());
    if (sourceQuery.usesResultSetAccessOptimization()) {
      targetQuery.setAccessors(sourceQuery.getAccessors());
    }
    ((ObjectLevelReadQuery)targetQuery).setRequiresDeferredLocks(sourceQuery.requiresDeferredLocks());
  }
  if (targetQuery.isObjectLevelReadQuery()) {
    if (sourceQuery.shouldCascadeAllParts() || (this.isPrivateOwned && sourceQuery.shouldCascadePrivateParts()) || (this.cascadeRefresh && sourceQuery.shouldCascadeByMapping())) {
      if (targetQuery == this.selectionQuery) {
        if (targetQuery.shouldPrepare()) {
          targetQuery.checkPrepare(executionSession,row);
        }
        targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
        targetQuery.setIsExecutionClone(true);
      }
      ((ObjectLevelReadQuery)targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
      targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
      if (targetQuery.shouldMaintainCache()) {
        targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
      }
      if (((ObjectLevelReadQuery)sourceQuery).hasAsOfClause()) {
        targetQuery.setSelectionCriteria((Expression)targetQuery.getSelectionCriteria().clone());
        ((ObjectLevelReadQuery)targetQuery).setAsOfClause(((ObjectLevelReadQuery)sourceQuery).getAsOfClause());
      }
    }
    if (isExtendingPessimisticLockScope(sourceQuery)) {
      if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.TARGET_QUERY) {
        if (targetQuery == this.selectionQuery) {
          if (targetQuery.shouldPrepare()) {
            targetQuery.checkPrepare(executionSession,row);
          }
          targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
          targetQuery.setIsExecutionClone(true);
        }
        extendPessimisticLockScopeInTargetQuery((ObjectLevelReadQuery)targetQuery,sourceQuery);
      }
 else       if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.DEDICATED_QUERY) {
        ReadQuery dedicatedQuery=getExtendPessimisticLockScopeDedicatedQuery(executionSession,sourceQuery.getLockMode());
        executionSession.executeQuery(dedicatedQuery,row);
      }
    }
  }
  targetQuery=prepareHistoricalQuery(targetQuery,sourceQuery,executionSession);
  return this.indirectionPolicy.valueFromQuery(targetQuery,row,executionSession);
}

Fixed version (ground truth):
/** 
 * INTERNAL: Return the value of the reference attribute or a value holder. Check whether the mapping's attribute should be optimized through batch and joining.
 * @param shouldUseSopObject indicates whether sopObject stored in the row should be used to extract the value (and fields/values stored in the row ignored).
 */
protected Object valueFromRowInternal(AbstractRecord row,JoinedAttributeManager joinManager,ObjectBuildingQuery sourceQuery,AbstractSession executionSession,boolean shouldUseSopObject) throws DatabaseException {
  ReadQuery targetQuery=this.selectionQuery;
  if (shouldUseSopObject) {
    Object sopAttribute=getAttributeValueFromObject(row.getSopObject());
    Object sopRealAttribute;
    if (isCollectionMapping()) {
      if (sopAttribute == null) {
        return getContainerPolicy().containerInstance();
      }
      sopRealAttribute=getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(),sopAttribute);
      if (getContainerPolicy().isEmpty(sopRealAttribute)) {
        return sopAttribute;
      }
    }
 else {
      if (sopAttribute == null) {
        return this.indirectionPolicy.nullValueFromRow();
      }
      sopRealAttribute=getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(),sopAttribute);
      if (sopRealAttribute == null) {
        return sopAttribute;
      }
    }
    DatabaseRecord sopRow=new DatabaseRecord(0);
    sopRow.setSopObject(sopRealAttribute);
    row=sopRow;
  }
  if (executionSession.isHistoricalSession() && !targetQuery.isPrepared()) {
    targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
    targetQuery.setIsExecutionClone(true);
  }
  if (targetQuery.isObjectLevelReadQuery() && targetQuery.getDescriptor().hasFetchGroupManager()) {
    FetchGroup sourceFG=sourceQuery.getExecutionFetchGroup(this.getDescriptor());
    if (sourceFG != null) {
      FetchGroup targetFetchGroup=sourceFG.getGroup(getAttributeName());
      if (targetFetchGroup != null) {
        if (targetQuery.shouldPrepare()) {
          targetQuery.checkPrepare(executionSession,row);
        }
        targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
        targetQuery.setIsExecutionClone(true);
        ((ObjectLevelReadQuery)targetQuery).setFetchGroup(targetFetchGroup);
      }
    }
  }
  if (!this.indirectionPolicy.usesIndirection() || shouldUseSopObject) {
    if (targetQuery == this.selectionQuery) {
      if (targetQuery.shouldPrepare()) {
        targetQuery.checkPrepare(executionSession,row);
      }
      targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
      targetQuery.setIsExecutionClone(true);
    }
    targetQuery.setQueryId(sourceQuery.getQueryId());
    if (sourceQuery.usesResultSetAccessOptimization()) {
      targetQuery.setAccessors(sourceQuery.getAccessors());
    }
    ((ObjectLevelReadQuery)targetQuery).setRequiresDeferredLocks(sourceQuery.requiresDeferredLocks());
  }
  if (targetQuery.isObjectLevelReadQuery()) {
    if (sourceQuery.shouldCascadeAllParts() || (this.isPrivateOwned && sourceQuery.shouldCascadePrivateParts()) || (this.cascadeRefresh && sourceQuery.shouldCascadeByMapping())) {
      if (targetQuery == this.selectionQuery) {
        if (targetQuery.shouldPrepare()) {
          targetQuery.checkPrepare(executionSession,row);
        }
        targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
        targetQuery.setIsExecutionClone(true);
      }
      ((ObjectLevelReadQuery)targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
      targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
      if (targetQuery.shouldMaintainCache()) {
        targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
      }
      if (((ObjectLevelReadQuery)sourceQuery).hasAsOfClause()) {
        targetQuery.setSelectionCriteria((Expression)targetQuery.getSelectionCriteria().clone());
        ((ObjectLevelReadQuery)targetQuery).setAsOfClause(((ObjectLevelReadQuery)sourceQuery).getAsOfClause());
      }
    }
    if (isExtendingPessimisticLockScope(sourceQuery)) {
      if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.TARGET_QUERY) {
        if (targetQuery == this.selectionQuery) {
          if (targetQuery.shouldPrepare()) {
            targetQuery.checkPrepare(executionSession,row);
          }
          targetQuery=(ObjectLevelReadQuery)targetQuery.clone();
          targetQuery.setIsExecutionClone(true);
        }
        extendPessimisticLockScopeInTargetQuery((ObjectLevelReadQuery)targetQuery,sourceQuery);
      }
 else       if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.DEDICATED_QUERY) {
        ReadQuery dedicatedQuery=getExtendPessimisticLockScopeDedicatedQuery(executionSession,sourceQuery.getLockMode());
        executionSession.executeQuery(dedicatedQuery,row);
      }
    }
  }
  targetQuery=prepareHistoricalQuery(targetQuery,sourceQuery,executionSession);
  return this.indirectionPolicy.valueFromQuery(targetQuery,row,executionSession);
}