Program: 176945
Project: 176945
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(PersistenceDelegator pd,EnhanceEntity e,Client client,Relation originalRelation,Field bidirectionalField,String relationName,EntityMetadata origMetadata,Object child,EntityMetadata childMetadata,Client childClient){
  if (bidirectionalField != null && child != null) {
    List obj=new ArrayList();
    Relation relation=childMetadata.getRelation(bidirectionalField.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(originalRelation.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];
            Object[] pKeys=client.findIdsByColumn(joinTableName,joinColumnName,inverseJoinColumnName,id,origMetadata.getEntityClazz());
            for (            Object key : pKeys) {
              String rlName=MetadataUtils.getMappedName(origMetadata,originalRelation);
              List keys=client.getColumnsById(joinTableName,joinColumnName,inverseJoinColumnName,key.toString());
              Object pEntity=client.find(origMetadata.getEntityClazz(),key);
              pEntity=pEntity != null && pEntity instanceof EnhanceEntity ? ((EnhanceEntity)pEntity).getEntity() : pEntity;
              List recursiveChilds=childClient.findAll(childMetadata.getEntityClazz(),keys.toArray());
              if (pEntity != null && recursiveChilds != null && !recursiveChilds.isEmpty()) {
                PropertyAccessorHelper.set(pEntity,originalRelation.getProperty(),getFieldInstance(recursiveChilds,originalRelation.getProperty()));
                if (results == null) {
                  results=new ArrayList<Object>();
                }
                results.add(pEntity);
              }
            }
          }
 else {
            results=client.findByRelation(relationName,id,origMetadata.getEntityClazz());
          }
        }
 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(),rowKey);
            if (result != null) {
              results.add(result);
            }
          }
        }
        if (results != null) {
          obj.addAll(results);
        }
      }
 catch (      PropertyAccessException ex) {
        log.error("String_Node_Str" + ex.getMessage());
        throw new EntityReaderException("String_Node_Str",ex);
      }
      for (      Object o : obj) {
        if (o != null && !relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
          Field f=originalRelation.getProperty();
          try {
            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);
            }
          }
 catch (          PropertyAccessException e1) {
            log.error("String_Node_Str" + e1.getMessage());
            throw new EntityReaderException("String_Node_Str",e1);
          }
        }
      }
    }
 else {
      obj.add(e.getEntity());
    }
    try {
      PropertyAccessorHelper.set(child,bidirectionalField,PropertyAccessorHelper.isCollection(bidirectionalField.getType()) ? getFieldInstance(obj,bidirectionalField) : e.getEntity());
    }
 catch (    PropertyAccessException ex) {
      log.error("String_Node_Str" + ex.getMessage());
      throw new EntityReaderException("String_Node_Str",ex);
    }
  }
}

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(PersistenceDelegator pd,EnhanceEntity e,Client client,Relation originalRelation,Field bidirectionalField,String relationName,EntityMetadata origMetadata,Object child,EntityMetadata childMetadata,Client childClient){
  if (bidirectionalField != null && child != null) {
    List obj=new ArrayList();
    Relation relation=childMetadata.getRelation(bidirectionalField.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(originalRelation.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];
            Object[] pKeys=client.findIdsByColumn(joinTableName,joinColumnName,inverseJoinColumnName,id,origMetadata.getEntityClazz());
            for (            Object key : pKeys) {
              String rlName=MetadataUtils.getMappedName(origMetadata,originalRelation);
              List keys=client.getColumnsById(joinTableName,joinColumnName,inverseJoinColumnName,key.toString());
              Object pEntity=client.find(origMetadata.getEntityClazz(),key);
              pEntity=pEntity != null && pEntity instanceof EnhanceEntity ? ((EnhanceEntity)pEntity).getEntity() : pEntity;
              List recursiveChilds=childClient.findAll(childMetadata.getEntityClazz(),keys.toArray());
              if (pEntity != null && recursiveChilds != null && !recursiveChilds.isEmpty()) {
                PropertyAccessorHelper.set(pEntity,originalRelation.getProperty(),getFieldInstance(recursiveChilds,originalRelation.getProperty()));
                if (results == null) {
                  results=new ArrayList<Object>();
                }
                results.add(pEntity);
              }
            }
          }
 else {
            results=client.findByRelation(relationName,id,origMetadata.getEntityClazz());
          }
        }
 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(),rowKey);
            if (result != null) {
              results.add(result instanceof EnhanceEntity ? ((EnhanceEntity)result).getEntity() : result);
            }
          }
        }
        if (results != null) {
          obj.addAll(results);
        }
      }
 catch (      PropertyAccessException ex) {
        log.error("String_Node_Str" + ex.getMessage());
        throw new EntityReaderException("String_Node_Str",ex);
      }
      for (      Object o : obj) {
        if (o != null && !relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
          o=o instanceof EnhanceEntity ? ((EnhanceEntity)o).getEntity() : o;
          Field f=originalRelation.getProperty();
          try {
            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);
            }
          }
 catch (          PropertyAccessException e1) {
            log.error("String_Node_Str" + e1.getMessage());
            throw new EntityReaderException("String_Node_Str",e1);
          }
        }
      }
    }
 else {
      obj.add(e.getEntity());
    }
    try {
      PropertyAccessorHelper.set(child,bidirectionalField,PropertyAccessorHelper.isCollection(bidirectionalField.getType()) ? getFieldInstance(obj,bidirectionalField) : e.getEntity());
    }
 catch (    PropertyAccessException ex) {
      log.error("String_Node_Str" + ex.getMessage());
      throw new EntityReaderException("String_Node_Str",ex);
    }
  }
}