Program: 68811
Project: 68811
Buggy source:
/** 
 * INTERNAL: Clone and prepare the selection query as a nested batch read query. This is used for nested batch reading.
 */
public ReadQuery prepareNestedBatchQuery(ReadAllQuery query){
  ReadAllQuery batchQuery=new ReadAllQuery();
  batchQuery.setReferenceClass(getReferenceClass());
  batchQuery.setSession(query.getSession());
  batchQuery.setShouldUseWrapperPolicy(false);
  if (query.shouldCascadeAllParts() || (query.shouldCascadePrivateParts() && isPrivateOwned()) || (query.shouldCascadeByMapping() && this.cascadeRefresh)) {
    batchQuery.setShouldRefreshIdentityMapResult(query.shouldRefreshIdentityMapResult());
    batchQuery.setCascadePolicy(query.getCascadePolicy());
    batchQuery.setShouldMaintainCache(query.shouldMaintainCache());
    batchQuery.setQueryId(query.getQueryId());
    if (query.hasAsOfClause()) {
      batchQuery.setAsOfClause(query.getAsOfClause());
    }
    batchQuery.setShouldBindAllParameters(query.getShouldBindAllParameters());
    batchQuery.setShouldPrepare(query.shouldPrepare());
  }
  ClassDescriptor descriptorToUse=query.getDescriptor();
  if ((descriptorToUse != getDescriptor()) && (!descriptorToUse.getMappings().contains(this)) && (!getDescriptor().isAggregateDescriptor())) {
    descriptorToUse=getDescriptor();
  }
  ExpressionBuilder builder=batchQuery.getExpressionBuilder();
  Expression backRef=builder.getManualQueryKey(getAttributeName() + "String_Node_Str",descriptorToUse);
  Expression twisted=backRef.twist(getSelectionCriteria(),builder);
  if (query.getSelectionCriteria() != null) {
    twisted=twisted.and(query.getSelectionCriteria().cloneUsing(backRef));
  }
  if (descriptorToUse.getQueryManager().getAdditionalJoinExpression() != null) {
    twisted=twisted.and(descriptorToUse.getQueryManager().getAdditionalJoinExpression().rebuildOn(backRef));
  }
  if (descriptorToUse.getHistoryPolicy() != null) {
    if (query.getSession().getAsOfClause() != null) {
      backRef.asOf(query.getSession().getAsOfClause());
    }
 else     if (batchQuery.getAsOfClause() == null) {
      backRef.asOf(AsOfClause.NO_CLAUSE);
    }
 else {
      backRef.asOf(batchQuery.getAsOfClause());
    }
    twisted=twisted.and(descriptorToUse.getHistoryPolicy().additionalHistoryExpression((ObjectExpression)backRef));
  }
  batchQuery.setSelectionCriteria(twisted);
  if (query.isDistinctComputed()) {
    batchQuery.setDistinctState(query.getDistinctState());
  }
  ReadQuery mappingQuery=this.getSelectionQuery();
  if (mappingQuery.isReadAllQuery()) {
    batchQuery.setOrderByExpressions(((Vector)((ReadAllQuery)mappingQuery).getOrderByExpressions().clone()));
    for (Enumeration enumtr=((ReadAllQuery)mappingQuery).getBatchReadAttributeExpressions().elements(); enumtr.hasMoreElements(); ) {
      Expression expression=((Expression)enumtr.nextElement()).rebuildOn(batchQuery.getExpressionBuilder());
      batchQuery.addBatchReadAttribute(expression);
    }
  }
  Vector nestedExpressions=extractNestedExpressions(query.getBatchReadAttributeExpressions(),batchQuery.getExpressionBuilder(),false);
  Helper.addAllToVector(batchQuery.getBatchReadAttributeExpressions(),nestedExpressions);
  postPrepareNestedBatchQuery(batchQuery,query);
  if (batchQuery.shouldPrepare()) {
    batchQuery.checkPrepare(query.getSession(),query.getTranslationRow());
  }
  batchQuery.setSession(null);
  return batchQuery;
}

Fixed version (ground truth):
/** 
 * INTERNAL: Clone and prepare the selection query as a nested batch read query. This is used for nested batch reading.
 */
public ReadQuery prepareNestedBatchQuery(ReadAllQuery query){
  ReadAllQuery batchQuery=new ReadAllQuery();
  batchQuery.setReferenceClass(getReferenceClass());
  batchQuery.setSession(query.getSession());
  batchQuery.setShouldUseWrapperPolicy(false);
  if (query.shouldCascadeAllParts() || (query.shouldCascadePrivateParts() && isPrivateOwned()) || (query.shouldCascadeByMapping() && this.cascadeRefresh)) {
    batchQuery.setShouldRefreshIdentityMapResult(query.shouldRefreshIdentityMapResult());
    batchQuery.setCascadePolicy(query.getCascadePolicy());
    batchQuery.setShouldMaintainCache(query.shouldMaintainCache());
    if (query.hasAsOfClause()) {
      batchQuery.setAsOfClause(query.getAsOfClause());
    }
    batchQuery.setShouldBindAllParameters(query.getShouldBindAllParameters());
    batchQuery.setShouldPrepare(query.shouldPrepare());
  }
  batchQuery.setQueryId(query.getQueryId());
  ClassDescriptor descriptorToUse=query.getDescriptor();
  if ((descriptorToUse != getDescriptor()) && (!descriptorToUse.getMappings().contains(this)) && (!getDescriptor().isAggregateDescriptor())) {
    descriptorToUse=getDescriptor();
  }
  ExpressionBuilder builder=batchQuery.getExpressionBuilder();
  Expression backRef=builder.getManualQueryKey(getAttributeName() + "String_Node_Str",descriptorToUse);
  Expression twisted=backRef.twist(getSelectionCriteria(),builder);
  if (query.getSelectionCriteria() != null) {
    twisted=twisted.and(query.getSelectionCriteria().cloneUsing(backRef));
  }
  if (descriptorToUse.getQueryManager().getAdditionalJoinExpression() != null) {
    twisted=twisted.and(descriptorToUse.getQueryManager().getAdditionalJoinExpression().rebuildOn(backRef));
  }
  if (descriptorToUse.getHistoryPolicy() != null) {
    if (query.getSession().getAsOfClause() != null) {
      backRef.asOf(query.getSession().getAsOfClause());
    }
 else     if (batchQuery.getAsOfClause() == null) {
      backRef.asOf(AsOfClause.NO_CLAUSE);
    }
 else {
      backRef.asOf(batchQuery.getAsOfClause());
    }
    twisted=twisted.and(descriptorToUse.getHistoryPolicy().additionalHistoryExpression((ObjectExpression)backRef));
  }
  batchQuery.setSelectionCriteria(twisted);
  if (query.isDistinctComputed()) {
    batchQuery.setDistinctState(query.getDistinctState());
  }
  ReadQuery mappingQuery=this.getSelectionQuery();
  if (mappingQuery.isReadAllQuery()) {
    batchQuery.setOrderByExpressions(((Vector)((ReadAllQuery)mappingQuery).getOrderByExpressions().clone()));
    for (Enumeration enumtr=((ReadAllQuery)mappingQuery).getBatchReadAttributeExpressions().elements(); enumtr.hasMoreElements(); ) {
      Expression expression=((Expression)enumtr.nextElement()).rebuildOn(batchQuery.getExpressionBuilder());
      batchQuery.addBatchReadAttribute(expression);
    }
  }
  Vector nestedExpressions=extractNestedExpressions(query.getBatchReadAttributeExpressions(),batchQuery.getExpressionBuilder(),false);
  Helper.addAllToVector(batchQuery.getBatchReadAttributeExpressions(),nestedExpressions);
  postPrepareNestedBatchQuery(batchQuery,query);
  if (batchQuery.shouldPrepare()) {
    batchQuery.checkPrepare(query.getSession(),query.getTranslationRow());
  }
  batchQuery.setSession(null);
  return batchQuery;
}