Program: 176928
Project: 176928
Buggy source:
/** 
 * Recursively fetches child entities for a given entity contained in  {@link EnhanceEntity}
 * @param e
 * @param client
 * @param m
 * @param pd
 * @return
 */
public Object recursivelyFindEntities(EnhanceEntity e,Client client,EntityMetadata m,PersistenceDelegator pd){
  Map<Object,Object> relationValuesMap=new HashMap<Object,Object>();
  Client childClient=null;
  Class childClass=null;
  EntityMetadata childMetadata=null;
  for (  Relation relation : m.getRelations()) {
    if (relation.isRelatedViaJoinTable()) {
      populateRelationFromJoinTable(e,childClient,m,pd,relation);
    }
 else {
      Relation.ForeignKey multiplicity=relation.getType();
      if (multiplicity.equals(Relation.ForeignKey.ONE_TO_ONE) || multiplicity.equals(Relation.ForeignKey.MANY_TO_ONE)) {
        String relationName=MetadataUtils.getMappedName(m,relation);
        Object relationValue=e.getRelations() != null ? e.getRelations().get(relationName) : null;
        childClass=relation.getTargetEntity();
        childMetadata=KunderaMetadataManager.getEntityMetadata(childClass);
        Field relationField=relation.getProperty();
        if (relationValue != null) {
          if (!relationValuesMap.containsKey(relationValue + childClass.getName()) && relationValue != null) {
            childClient=pd.getClient(childMetadata);
            Object child=null;
            if (relationValue != null) {
              if (childClass.equals(e.getEntity().getClass())) {
                child=childClient.find(childClass,relationValue.toString());
              }
 else {
                child=pd.find(childClass,relationValue.toString());
              }
            }
            relationValuesMap.put(relationValue + childClass.getName(),child);
          }
          Field biDirectionalField=getBiDirectionalField(e.getEntity().getClass(),childClass);
          onBiDirection(pd,e,client,relation,biDirectionalField,relation.getJoinColumnName(),m,relationValuesMap.get(relationValue + childClass.getName()),childMetadata,childClient);
          List<Object> collection=new ArrayList<Object>(1);
          collection.add(relationValuesMap.get(relationValue + childClass.getName()));
          PropertyAccessorHelper.set(e.getEntity(),relationField,PropertyAccessorHelper.isCollection(relationField.getType()) ? getFieldInstance(collection,relationField) : collection.get(0));
        }
      }
 else       if (multiplicity.equals(Relation.ForeignKey.ONE_TO_MANY)) {
        childClass=relation.getTargetEntity();
        childMetadata=pd.getMetadata(childClass);
        Field biDirectionalField=getBiDirectionalField(e.getEntity().getClass(),childClass);
        childClient=pd.getClient(childMetadata);
        String relationName=biDirectionalField != null ? m.getIdColumn().getName() : MetadataUtils.getMappedName(m,relation);
        String relationalValue=e.getEntityId();
        Field f=relation.getProperty();
        if (relationName != null && relationalValue != null) {
          if (!relationValuesMap.containsKey(relationalValue + childClass.getName())) {
            List<Object> childs=null;
            if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
              childs=childClient.findByRelation(relationName,relationalValue,childClass);
            }
 else {
              if (relation.isJoinedByPrimaryKey()) {
                childs=new ArrayList();
                childs.add(childClass.equals(e.getEntity().getClass()) ? childs.add(childClient.find(childClass,e.getEntityId())) : pd.find(childClass,relationalValue.toString()));
              }
 else {
                String query=getQuery(DocumentIndexer.PARENT_ID_CLASS,e.getEntity().getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,e.getEntityId(),childClass.getCanonicalName().toLowerCase());
                Map<String,String> results=childClient.getIndexManager().search(query);
                Set<String> rsSet=new HashSet<String>(results.values());
                if (childClass.equals(e.getEntity().getClass())) {
                  childs=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
                }
 else {
                  childs=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
                }
              }
            }
            relationValuesMap.put(relationalValue + childClass.getName(),childs);
            if (childs != null) {
              for (              Object child : childs) {
                onBiDirection(pd,e,client,relation,biDirectionalField,relation.getJoinColumnName(),m,child,childMetadata,childClient);
              }
            }
          }
        }
        onReflect(e.getEntity(),f,(List)relationValuesMap.get(relationalValue + childClass.getName()));
      }
    }
  }
  return e.getEntity();
}

Fixed version (ground truth):
/** 
 * Recursively fetches child entities for a given entity contained in  {@link EnhanceEntity}
 * @param e
 * @param client
 * @param m
 * @param pd
 * @return
 */
public Object recursivelyFindEntities(EnhanceEntity e,Client client,EntityMetadata m,PersistenceDelegator pd){
  Map<Object,Object> relationValuesMap=new HashMap<Object,Object>();
  Client childClient=null;
  Class childClass=null;
  EntityMetadata childMetadata=null;
  for (  Relation relation : m.getRelations()) {
    if (relation.isRelatedViaJoinTable()) {
      populateRelationFromJoinTable(e,childClient,m,pd,relation);
    }
 else {
      Relation.ForeignKey multiplicity=relation.getType();
      if (multiplicity.equals(Relation.ForeignKey.ONE_TO_ONE) || multiplicity.equals(Relation.ForeignKey.MANY_TO_ONE)) {
        String relationName=MetadataUtils.getMappedName(m,relation);
        Object relationValue=e.getRelations() != null ? e.getRelations().get(relationName) : null;
        childClass=relation.getTargetEntity();
        childMetadata=KunderaMetadataManager.getEntityMetadata(childClass);
        Field relationField=relation.getProperty();
        if (relationValue != null) {
          if (!relationValuesMap.containsKey(relationValue + childClass.getName()) && relationValue != null) {
            childClient=pd.getClient(childMetadata);
            Object child=null;
            if (relationValue != null) {
              if (childClass.equals(e.getEntity().getClass())) {
                child=childClient.find(childClass,relationValue.toString());
              }
 else {
                child=pd.find(childClass,relationValue.toString());
              }
            }
            relationValuesMap.put(relationValue + childClass.getName(),child);
          }
          Field biDirectionalField=getBiDirectionalField(e.getEntity().getClass(),childClass);
          onBiDirection(pd,e,client,relation,biDirectionalField,relation.getJoinColumnName(),m,relationValuesMap.get(relationValue + childClass.getName()),childMetadata,childClient);
          List<Object> collection=new ArrayList<Object>(1);
          collection.add(relationValuesMap.get(relationValue + childClass.getName()));
          PropertyAccessorHelper.set(e.getEntity(),relationField,PropertyAccessorHelper.isCollection(relationField.getType()) ? getFieldInstance(collection,relationField) : collection.get(0));
        }
      }
 else       if (multiplicity.equals(Relation.ForeignKey.ONE_TO_MANY)) {
        childClass=relation.getTargetEntity();
        childMetadata=pd.getMetadata(childClass);
        Field biDirectionalField=getBiDirectionalField(e.getEntity().getClass(),childClass);
        childClient=pd.getClient(childMetadata);
        String relationName=biDirectionalField != null ? m.getIdColumn().getName() : MetadataUtils.getMappedName(m,relation);
        String relationalValue=e.getEntityId();
        Field f=relation.getProperty();
        if (relationName != null && relationalValue != null) {
          if (!relationValuesMap.containsKey(relationalValue + childClass.getName())) {
            List<Object> childs=null;
            if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
              childs=childClient.findByRelation(relationName,relationalValue,childClass);
            }
 else {
              if (relation.isJoinedByPrimaryKey()) {
                childs=new ArrayList();
                childs.add(childClass.equals(e.getEntity().getClass()) ? childs.add(childClient.find(childClass,e.getEntityId())) : pd.find(childClass,relationalValue.toString()));
              }
 else {
                String query=getQuery(DocumentIndexer.PARENT_ID_CLASS,e.getEntity().getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,e.getEntityId(),childClass.getCanonicalName().toLowerCase());
                Map<String,String> results=childClient.getIndexManager().search(query);
                Set<String> rsSet=new HashSet<String>(results.values());
                if (childClass.equals(e.getEntity().getClass())) {
                  childs=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
                }
 else {
                  childs=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
                }
              }
            }
            relationValuesMap.put(relationalValue + childClass.getName(),childs);
            if (childs != null) {
              for (              Object child : childs) {
                recursivelyFindEntities(new EnhanceEntity(child,PropertyAccessorHelper.getId(child,childMetadata),null),childClient,childMetadata,pd);
                onBiDirection(pd,e,client,relation,biDirectionalField,relation.getJoinColumnName(),m,child,childMetadata,childClient);
              }
            }
          }
        }
        onReflect(e.getEntity(),f,(List)relationValuesMap.get(relationalValue + childClass.getName()));
      }
    }
  }
  return e.getEntity();
}