Program: 176901
Project: 176901
Buggy source:
/** 
 * @param entity
 * @param pd
 * @param entityId
 * @param relation
 * @param relationName
 */
void populateRelationViaQuery(Object entity,PersistenceDelegator pd,String entityId,Relation relation,String relationName,EntityMetadata childMetadata){
  Class<?> childClass=relation.getTargetEntity();
  Client childClient=pd.getClient(childMetadata);
  List children=null;
  if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
    children=pd.find(childClass,entityId,relationName);
  }
 else {
    String query=LuceneQueryUtils.getQuery(DocumentIndexer.PARENT_ID_CLASS,entity.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,entityId,childClass.getCanonicalName().toLowerCase());
    Map<String,String> results=childClient.getIndexManager().search(query);
    Set<String> rsSet=new HashSet<String>(results.values());
    if (childClass.equals(entity.getClass())) {
      children=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
    }
 else {
      children=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
    }
  }
  onReflect(entity,relation.getProperty(),children);
  Field biDirectionalField=getBiDirectionalField(entity.getClass(),relation.getTargetEntity());
  boolean isBidirectionalRelation=(biDirectionalField != null);
  if (isBidirectionalRelation && children != null) {
    Relation reverseRelation=childMetadata.getRelation(biDirectionalField.getName());
    for (    Object child : children) {
      PropertyAccessorHelper.set(child,reverseRelation.getProperty(),entity);
    }
  }
}

Fixed version (ground truth):
/** 
 * @param entity
 * @param pd
 * @param entityId
 * @param relation
 * @param relationName
 */
void populateRelationViaQuery(Object entity,PersistenceDelegator pd,String entityId,Relation relation,String relationName,EntityMetadata childMetadata){
  Class<?> childClass=relation.getTargetEntity();
  Client childClient=pd.getClient(childMetadata);
  List children=null;
  if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
    children=pd.find(childClass,entityId,relationName);
  }
 else {
    String query=LuceneQueryUtils.getQuery(DocumentIndexer.PARENT_ID_CLASS,entity.getClass().getCanonicalName().toLowerCase(),DocumentIndexer.PARENT_ID_FIELD,entityId,childClass.getCanonicalName().toLowerCase());
    Map<String,String> results=childClient.getIndexManager().search(query);
    Set<String> rsSet=new HashSet<String>(results.values());
    if (childClass.equals(entity.getClass())) {
      children=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
    }
 else {
      children=(List<Object>)childClient.findAll(childClass,rsSet.toArray(new String[]{}));
    }
  }
  onReflect(entity,relation.getProperty(),children);
  Field biDirectionalField=getBiDirectionalField(entity.getClass(),relation.getTargetEntity());
  boolean isBidirectionalRelation=(biDirectionalField != null);
  if (isBidirectionalRelation && children != null) {
    Relation reverseRelation=childMetadata.getRelation(biDirectionalField.getName());
    for (    Object child : children) {
      PropertyAccessorHelper.set(child,reverseRelation.getProperty(),entity);
    }
  }
  if (children != null) {
    MainCache mainCache=(MainCache)pd.getPersistenceCache().getMainCache();
    for (    Object child : children) {
      Object childId=PropertyAccessorHelper.getId(child,childMetadata);
      String nodeId=ObjectGraphUtils.getNodeId(childId,childMetadata.getEntityClazz());
      Node node=new Node(nodeId,childMetadata.getEntityClazz(),new ManagedState(),pd.getPersistenceCache());
      node.setData(child);
      node.setPersistenceDelegator(pd);
      mainCache.addNodeToCache(node);
    }
  }
}