Program: 176979
Project: 176979
Buggy source:
/** 
 * On bi direction.
 * @param e the e
 * @param client the client
 * @param objectGraph the object graph
 * @param origMetadata the orig metadata
 * @param child the child
 * @param childMetadata the child metadata
 * @param childClient the child client
 * @throws Exception the exception
 */
private void onBiDirection(EnhanceEntity e,Client client,EntitySaveGraph objectGraph,EntityMetadata origMetadata,Object child,EntityMetadata childMetadata,Client childClient) throws Exception {
  if (!objectGraph.isUniDirectional() && child != null) {
    List obj=new ArrayList();
    Relation relation=childMetadata.getRelation(objectGraph.getBidirectionalProperty().getName());
    if (relation.getType().equals(ForeignKey.ONE_TO_MANY) || relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
      String query=null;
      try {
        String id=PropertyAccessorHelper.getId(child,childMetadata);
        List<Object> results=null;
        if (MetadataUtils.useSecondryIndex(client.getPersistenceUnit())) {
          if (origMetadata.isRelationViaJoinTable()) {
            Relation joinTableRelation=origMetadata.getRelation(objectGraph.getProperty().getName());
            JoinTableMetadata jtMetadata=joinTableRelation.getJoinTableMetadata();
            String joinTableName=jtMetadata.getJoinTableName();
            Set<String> joinColumns=jtMetadata.getJoinColumns();
            Set<String> inverseJoinColumns=jtMetadata.getInverseJoinColumns();
            String joinColumnName=(String)joinColumns.toArray()[0];
            String inverseJoinColumnName=(String)inverseJoinColumns.toArray()[0];
            List<Object> parentEntities=client.findParentEntityFromJoinTable(origMetadata,joinTableName,joinColumnName,inverseJoinColumnName,id);
            if (results == null) {
              results=new ArrayList<Object>();
            }
            if (parentEntities != null) {
              results.addAll(parentEntities);
            }
          }
 else {
            results=client.find(objectGraph.getfKeyName(),id,origMetadata);
          }
        }
 else {
          Map<String,String> keys=null;
          if (relation.getType().equals(ForeignKey.ONE_TO_MANY)) {
            query=getQuery(DocumentIndexer.PARENT_ID_CLASS,child.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,id,e.getEntity().getClass().getCanonicalName().toLowerCase());
            keys=client.getIndexManager().search(query);
          }
 else {
            query=getQuery(DocumentIndexer.ENTITY_CLASS_FIELD,child.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.ENTITY_ID_FIELD,id,null);
            keys=client.getIndexManager().fetchRelation(query);
          }
          Set<String> uqSet=new HashSet<String>(keys.values());
          results=new ArrayList<Object>();
          for (          String rowKey : uqSet) {
            results.add(client.find(e.getEntity().getClass(),origMetadata,rowKey,null));
          }
        }
        if (results != null) {
          obj.addAll(results);
        }
      }
 catch (      PropertyAccessException ex) {
        log.error("String_Node_Str" + ex.getMessage());
        throw new QueryHandlerException(ex.getMessage());
      }
      for (      Object o : obj) {
        if (o != null) {
          Field f=objectGraph.getProperty();
          if (PropertyAccessorHelper.isCollection(f.getType())) {
            List l=new ArrayList();
            l.add(child);
            Object oo=getFieldInstance(l,f);
            PropertyAccessorHelper.set(o,f,oo);
          }
 else {
            PropertyAccessorHelper.set(o,f,child);
          }
        }
      }
    }
 else {
      obj.add(e.getEntity());
    }
    try {
      PropertyAccessorHelper.set(child,objectGraph.getBidirectionalProperty(),PropertyAccessorHelper.isCollection(objectGraph.getBidirectionalProperty().getType()) ? getFieldInstance(obj,objectGraph.getBidirectionalProperty()) : e.getEntity());
    }
 catch (    PropertyAccessException ex) {
      log.error("String_Node_Str" + ex.getMessage());
      throw new QueryHandlerException(ex.getMessage());
    }
  }
}

Fixed version (ground truth):
/** 
 * On bi direction.
 * @param e the e
 * @param client the client
 * @param objectGraph the object graph
 * @param origMetadata the orig metadata
 * @param child the child
 * @param childMetadata the child metadata
 * @param childClient the child client
 * @throws Exception the exception
 */
private void onBiDirection(EnhanceEntity e,Client client,EntitySaveGraph objectGraph,EntityMetadata origMetadata,Object child,EntityMetadata childMetadata,Client childClient) throws Exception {
  if (!objectGraph.isUniDirectional() && child != null) {
    List obj=new ArrayList();
    Relation relation=childMetadata.getRelation(objectGraph.getBidirectionalProperty().getName());
    if (relation.getType().equals(ForeignKey.ONE_TO_MANY) || relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
      String query=null;
      try {
        String id=PropertyAccessorHelper.getId(child,childMetadata);
        List<Object> results=null;
        if (MetadataUtils.useSecondryIndex(client.getPersistenceUnit())) {
          if (origMetadata.isRelationViaJoinTable()) {
            Relation joinTableRelation=origMetadata.getRelation(objectGraph.getProperty().getName());
            JoinTableMetadata jtMetadata=joinTableRelation.getJoinTableMetadata();
            String joinTableName=jtMetadata.getJoinTableName();
            Set<String> joinColumns=jtMetadata.getJoinColumns();
            Set<String> inverseJoinColumns=jtMetadata.getInverseJoinColumns();
            String joinColumnName=(String)joinColumns.toArray()[0];
            String inverseJoinColumnName=(String)inverseJoinColumns.toArray()[0];
            List<Object> parentEntities=client.findParentEntityFromJoinTable(origMetadata,joinTableName,joinColumnName,inverseJoinColumnName,id);
            if (results == null) {
              results=new ArrayList<Object>();
            }
            if (parentEntities != null) {
              results.addAll(parentEntities);
            }
          }
 else {
            results=client.find(objectGraph.getfKeyName(),id,origMetadata);
          }
        }
 else {
          Map<String,String> keys=null;
          if (relation.getType().equals(ForeignKey.ONE_TO_MANY)) {
            query=getQuery(DocumentIndexer.PARENT_ID_CLASS,child.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,id,e.getEntity().getClass().getCanonicalName().toLowerCase());
            keys=client.getIndexManager().search(query);
          }
 else {
            query=getQuery(DocumentIndexer.ENTITY_CLASS_FIELD,child.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.ENTITY_ID_FIELD,id,null);
            keys=client.getIndexManager().fetchRelation(query);
          }
          Set<String> uqSet=new HashSet<String>(keys.values());
          results=new ArrayList<Object>();
          for (          String rowKey : uqSet) {
            Object result=client.find(e.getEntity().getClass(),origMetadata,rowKey,null);
            if (result != null) {
              results.add(result);
            }
          }
        }
        if (results != null) {
          obj.addAll(results);
        }
      }
 catch (      PropertyAccessException ex) {
        log.error("String_Node_Str" + ex.getMessage());
        throw new QueryHandlerException(ex.getMessage());
      }
      for (      Object o : obj) {
        if (o != null) {
          Field f=objectGraph.getProperty();
          if (PropertyAccessorHelper.isCollection(f.getType())) {
            List l=new ArrayList();
            l.add(child);
            Object oo=getFieldInstance(l,f);
            PropertyAccessorHelper.set(o,f,oo);
          }
 else {
            PropertyAccessorHelper.set(o,f,child);
          }
        }
      }
    }
 else {
      obj.add(e.getEntity());
    }
    try {
      PropertyAccessorHelper.set(child,objectGraph.getBidirectionalProperty(),PropertyAccessorHelper.isCollection(objectGraph.getBidirectionalProperty().getType()) ? getFieldInstance(obj,objectGraph.getBidirectionalProperty()) : e.getEntity());
    }
 catch (    PropertyAccessException ex) {
      log.error("String_Node_Str" + ex.getMessage());
      throw new QueryHandlerException(ex.getMessage());
    }
  }
}