Program: 63306
Project: 63306
Buggy source:
/** 
 * INTERNAL: Build and return an aggregate object from the specified row. If a null value is allowed and all the appropriate fields in the row are NULL, return a null. If an aggregate is referenced by the target object, return it (maintain identity) Otherwise, simply create a new aggregate object and return it.
 */
public Object buildAggregateFromRow(AbstractRecord databaseRow,Object targetObject,CacheKey cacheKey,JoinedAttributeManager joinManager,ObjectBuildingQuery sourceQuery,boolean buildShallowOriginal,AbstractSession executionSession,boolean targetIsProtected) throws DatabaseException {
  if (databaseRow.hasSopObject()) {
    Object sopAggregate=getAttributeValueFromObject(databaseRow.getSopObject());
    if ((targetObject != null) && (targetObject != databaseRow.getSopObject())) {
      setAttributeValueInObject(targetObject,sopAggregate);
    }
    return sopAggregate;
  }
  if (isNullAllowed() && allAggregateFieldsAreNull(databaseRow)) {
    return null;
  }
  Object aggregate=null;
  ClassDescriptor descriptor=getReferenceDescriptor();
  boolean refreshing=true;
  if (targetObject != null) {
    if (descriptor.hasInheritance()) {
      Class newAggregateClass=descriptor.getInheritancePolicy().classFromRow(databaseRow,executionSession);
      descriptor=getReferenceDescriptor(newAggregateClass,executionSession);
      aggregate=getMatchingAttributeValueFromObject(databaseRow,targetObject,executionSession,descriptor);
      if ((aggregate != null) && (aggregate.getClass() != newAggregateClass)) {
        aggregate=descriptor.getObjectBuilder().buildNewInstance();
        refreshing=false;
      }
    }
 else {
      aggregate=getMatchingAttributeValueFromObject(databaseRow,targetObject,executionSession,descriptor);
    }
  }
  if (aggregate == null) {
    aggregate=descriptor.getObjectBuilder().buildNewInstance();
    refreshing=false;
  }
  ObjectBuildingQuery nestedQuery=prepareNestedQuery(sourceQuery);
  FetchGroup targetFetchGroup=null;
  if (nestedQuery.isObjectLevelReadQuery()) {
    targetFetchGroup=((ObjectLevelReadQuery)nestedQuery).getFetchGroup();
    if (refreshing && descriptor.hasFetchGroupManager()) {
      descriptor.getFetchGroupManager().unionEntityFetchGroupIntoObject(aggregate,descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup),executionSession,true);
    }
  }
  if (buildShallowOriginal) {
    descriptor.getObjectBuilder().buildAttributesIntoShallowObject(aggregate,databaseRow,nestedQuery);
  }
 else   if (executionSession.isUnitOfWork()) {
    descriptor.getObjectBuilder().buildAttributesIntoWorkingCopyClone(aggregate,buildWrapperCacheKeyForAggregate(cacheKey,targetIsProtected),nestedQuery,joinManager,databaseRow,(UnitOfWorkImpl)executionSession,refreshing);
  }
 else {
    descriptor.getObjectBuilder().buildAttributesIntoObject(aggregate,buildWrapperCacheKeyForAggregate(cacheKey,targetIsProtected),databaseRow,nestedQuery,joinManager,nestedQuery.getExecutionFetchGroup(descriptor),refreshing,executionSession);
  }
  if ((targetFetchGroup != null) && descriptor.hasFetchGroupManager() && !refreshing&& sourceQuery.shouldMaintainCache()&& !sourceQuery.shouldStoreBypassCache()) {
    EntityFetchGroup entityFetchGroup=descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup);
    if (entityFetchGroup != null) {
      entityFetchGroup=(EntityFetchGroup)entityFetchGroup.clone();
      entityFetchGroup.setRootEntity((FetchGroupTracker)cacheKey.getObject());
      entityFetchGroup.setOnEntity(aggregate,executionSession);
    }
  }
  return aggregate;
}

Fixed version (ground truth):
/** 
 * INTERNAL: Build and return an aggregate object from the specified row. If a null value is allowed and all the appropriate fields in the row are NULL, return a null. If an aggregate is referenced by the target object, return it (maintain identity) Otherwise, simply create a new aggregate object and return it.
 */
public Object buildAggregateFromRow(AbstractRecord databaseRow,Object targetObject,CacheKey cacheKey,JoinedAttributeManager joinManager,ObjectBuildingQuery sourceQuery,boolean buildShallowOriginal,AbstractSession executionSession,boolean targetIsProtected) throws DatabaseException {
  if (databaseRow.hasSopObject()) {
    Object sopAggregate=getAttributeValueFromObject(databaseRow.getSopObject());
    if ((targetObject != null) && (targetObject != databaseRow.getSopObject())) {
      setAttributeValueInObject(targetObject,sopAggregate);
    }
    return sopAggregate;
  }
  if (isNullAllowed() && allAggregateFieldsAreNull(databaseRow)) {
    return null;
  }
  Object aggregate=null;
  ClassDescriptor descriptor=getReferenceDescriptor();
  boolean refreshing=true;
  if (targetObject != null) {
    if (descriptor.hasInheritance()) {
      Class newAggregateClass=descriptor.getInheritancePolicy().classFromRow(databaseRow,executionSession);
      descriptor=getReferenceDescriptor(newAggregateClass,executionSession);
      aggregate=getMatchingAttributeValueFromObject(databaseRow,targetObject,executionSession,descriptor);
      if ((aggregate != null) && (aggregate.getClass() != newAggregateClass)) {
        aggregate=descriptor.getObjectBuilder().buildNewInstance();
        refreshing=false;
      }
    }
 else {
      aggregate=getMatchingAttributeValueFromObject(databaseRow,targetObject,executionSession,descriptor);
    }
  }
  if (aggregate == null || (aggregate != null && cacheKey == null)) {
    aggregate=descriptor.getObjectBuilder().buildNewInstance();
    refreshing=false;
  }
  ObjectBuildingQuery nestedQuery=prepareNestedQuery(sourceQuery);
  FetchGroup targetFetchGroup=null;
  if (nestedQuery.isObjectLevelReadQuery()) {
    targetFetchGroup=((ObjectLevelReadQuery)nestedQuery).getFetchGroup();
    if (refreshing && descriptor.hasFetchGroupManager()) {
      descriptor.getFetchGroupManager().unionEntityFetchGroupIntoObject(aggregate,descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup),executionSession,true);
    }
  }
  if (buildShallowOriginal) {
    descriptor.getObjectBuilder().buildAttributesIntoShallowObject(aggregate,databaseRow,nestedQuery);
  }
 else   if (executionSession.isUnitOfWork()) {
    descriptor.getObjectBuilder().buildAttributesIntoWorkingCopyClone(aggregate,buildWrapperCacheKeyForAggregate(cacheKey,targetIsProtected),nestedQuery,joinManager,databaseRow,(UnitOfWorkImpl)executionSession,refreshing);
  }
 else {
    descriptor.getObjectBuilder().buildAttributesIntoObject(aggregate,buildWrapperCacheKeyForAggregate(cacheKey,targetIsProtected),databaseRow,nestedQuery,joinManager,nestedQuery.getExecutionFetchGroup(descriptor),refreshing,executionSession);
  }
  if ((targetFetchGroup != null) && descriptor.hasFetchGroupManager() && cacheKey != null && !refreshing && sourceQuery.shouldMaintainCache() && !sourceQuery.shouldStoreBypassCache()) {
    EntityFetchGroup entityFetchGroup=descriptor.getFetchGroupManager().getEntityFetchGroup(targetFetchGroup);
    if (entityFetchGroup != null) {
      entityFetchGroup=(EntityFetchGroup)entityFetchGroup.clone();
      entityFetchGroup.setRootEntity((FetchGroupTracker)cacheKey.getObject());
      entityFetchGroup.setOnEntity(aggregate,executionSession);
    }
  }
  return aggregate;
}