Program: 176997
Project: 176997
Buggy source:
/** 
 * Compute graph.
 * @param e the e
 * @param graphs the graphs
 * @return the object
 * @throws Exception the exception
 */
public Object computeGraph(EnhanceEntity e,List<EntitySaveGraph> graphs,Map<Object,Object> collectionHolder,Client client,EntityMetadata m,PersistenceDelegator persistenceDelegeator) throws Exception {
  Client childClient=null;
  Class<?> childClazz=null;
  EntityMetadata childMetadata=null;
  for (  EntitySaveGraph g : graphs) {
    Relation relation=m.getRelation(g.getProperty().getName());
    if (relation.isRelatedViaJoinTable()) {
      computeJoinTableRelations(e,client,m,g,persistenceDelegeator,relation);
    }
 else {
      if (e.getEntity().getClass().equals(g.getChildClass())) {
        String relationName=g.getfKeyName();
        Object relationalValue=e.getRelations().get(relationName);
        childClazz=g.getParentClass();
        childMetadata=persistenceDelegeator.getMetadata(childClazz);
        Field f=g.getProperty();
        if (!collectionHolder.containsKey(relationalValue)) {
          childClient=persistenceDelegeator.getClient(childMetadata);
          Object child=null;
          if (childClazz.equals(e.getEntity().getClass())) {
            child=childClient.find(childClazz,childMetadata,relationalValue.toString(),null);
          }
 else {
            child=persistenceDelegeator.find(childClazz,relationalValue.toString(),g);
          }
          collectionHolder.put(relationalValue,child);
        }
        onBiDirection(e,client,g,m,collectionHolder.get(relationalValue),childMetadata,childClient);
        List<Object> collection=new ArrayList<Object>(1);
        collection.add(collectionHolder.get(relationalValue));
        PropertyAccessorHelper.set(e.getEntity(),f,PropertyAccessorHelper.isCollection(f.getType()) ? getFieldInstance(collection,f) : collection.get(0));
      }
 else {
        childClazz=g.getChildClass();
        childMetadata=persistenceDelegeator.getMetadata(childClazz);
        childClient=persistenceDelegeator.getClient(childMetadata);
        String relationName=g.getfKeyName();
        String relationalValue=e.getEntityId();
        Field f=g.getProperty();
        if (!collectionHolder.containsKey(relationalValue)) {
          List<Object> childs=null;
          if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
            childs=childClient.find(relationName,relationalValue,childMetadata);
          }
 else {
            if (g.isSharedPrimaryKey()) {
              childs=new ArrayList();
              childs.add(childClazz.equals(e.getEntity().getClass()) ? childs.add(childClient.find(childClazz,childMetadata,e.getEntityId(),null)) : persistenceDelegeator.find(childClazz,relationalValue.toString()));
            }
 else {
              String query=getQuery(DocumentIndexer.PARENT_ID_CLASS,e.getEntity().getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,e.getEntityId());
              Map<String,String> results=childClient.getIndexManager().search(query);
              Set<String> rsSet=new HashSet<String>(results.values());
              if (childClazz.equals(e.getEntity().getClass())) {
                childs=(List<Object>)childClient.findAll(childClazz,rsSet.toArray(new String[]{}));
              }
 else {
                childs=(List<Object>)persistenceDelegeator.find(childClazz,g,rsSet.toArray(new String[]{}));
              }
            }
          }
          collectionHolder.put(relationalValue,childs);
          if (childs != null) {
            for (            Object child : childs) {
              onBiDirection(e,client,g,m,child,childMetadata,childClient);
            }
          }
        }
        onReflect(e.getEntity(),f,(List)collectionHolder.get(relationalValue));
      }
    }
  }
  return e.getEntity();
}

Fixed version (ground truth):
/** 
 * Compute graph.
 * @param e the e
 * @param graphs the graphs
 * @return the object
 * @throws Exception the exception
 */
public Object computeGraph(EnhanceEntity e,List<EntitySaveGraph> graphs,Map<Object,Object> collectionHolder,Client client,EntityMetadata m,PersistenceDelegator persistenceDelegeator) throws Exception {
  Client childClient=null;
  Class<?> childClazz=null;
  EntityMetadata childMetadata=null;
  for (  EntitySaveGraph g : graphs) {
    Relation relation=m.getRelation(g.getProperty().getName());
    if (relation.isRelatedViaJoinTable()) {
      computeJoinTableRelations(e,client,m,g,persistenceDelegeator,relation);
    }
 else {
      if (e.getEntity().getClass().equals(g.getChildClass())) {
        String relationName=g.getfKeyName();
        Object relationalValue=e.getRelations().get(relationName);
        childClazz=g.getParentClass();
        childMetadata=persistenceDelegeator.getMetadata(childClazz);
        Field f=g.getProperty();
        if (!collectionHolder.containsKey(relationalValue)) {
          childClient=persistenceDelegeator.getClient(childMetadata);
          Object child=null;
          if (childClazz.equals(e.getEntity().getClass())) {
            child=childClient.find(childClazz,childMetadata,relationalValue.toString(),null);
          }
 else {
            child=persistenceDelegeator.find(childClazz,relationalValue.toString(),g);
          }
          collectionHolder.put(relationalValue,child);
        }
        onBiDirection(e,client,g,m,collectionHolder.get(relationalValue),childMetadata,childClient);
        List<Object> collection=new ArrayList<Object>(1);
        collection.add(collectionHolder.get(relationalValue));
        PropertyAccessorHelper.set(e.getEntity(),f,PropertyAccessorHelper.isCollection(f.getType()) ? getFieldInstance(collection,f) : collection.get(0));
      }
 else {
        childClazz=g.getChildClass();
        childMetadata=persistenceDelegeator.getMetadata(childClazz);
        childClient=persistenceDelegeator.getClient(childMetadata);
        String relationName=g.getfKeyName();
        String relationalValue=e.getEntityId();
        Field f=g.getProperty();
        if (!collectionHolder.containsKey(relationalValue)) {
          List<Object> childs=null;
          if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
            childs=childClient.find(relationName,relationalValue,childMetadata);
          }
 else {
            if (g.isSharedPrimaryKey()) {
              childs=new ArrayList();
              childs.add(childClazz.equals(e.getEntity().getClass()) ? childs.add(childClient.find(childClazz,childMetadata,e.getEntityId(),null)) : persistenceDelegeator.find(childClazz,relationalValue.toString()));
            }
 else {
              String query=getQuery(DocumentIndexer.PARENT_ID_CLASS,e.getEntity().getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,e.getEntityId(),childClazz.getCanonicalName().toLowerCase());
              Map<String,String> results=childClient.getIndexManager().search(query);
              Set<String> rsSet=new HashSet<String>(results.values());
              if (childClazz.equals(e.getEntity().getClass())) {
                childs=(List<Object>)childClient.findAll(childClazz,rsSet.toArray(new String[]{}));
              }
 else {
                childs=(List<Object>)persistenceDelegeator.find(childClazz,g,rsSet.toArray(new String[]{}));
              }
            }
          }
          collectionHolder.put(relationalValue,childs);
          if (childs != null) {
            for (            Object child : childs) {
              onBiDirection(e,client,g,m,child,childMetadata,childClient);
            }
          }
        }
        onReflect(e.getEntity(),f,(List)collectionHolder.get(relationalValue));
      }
    }
  }
  return e.getEntity();
}