Program: 176908
Project: 176908
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);
    }
  }
  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);
    }
  }
}

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 associatedEntities=null;
  if (MetadataUtils.useSecondryIndex(childClient.getPersistenceUnit())) {
    associatedEntities=pd.find(childClass,entityId,relationName);
  }
 else {
    associatedEntities=getAssociatedEntitiesFromLucene(entity,entityId,childClass,childClient);
  }
  if (associatedEntities != null && !associatedEntities.isEmpty()) {
    setAssociatedEntities(entity,relation.getProperty(),associatedEntities);
  }
  Field biDirectionalField=getBiDirectionalField(entity.getClass(),relation.getTargetEntity());
  boolean isBidirectionalRelation=(biDirectionalField != null);
  if (isBidirectionalRelation && associatedEntities != null) {
    Relation reverseRelation=childMetadata.getRelation(biDirectionalField.getName());
    for (    Object child : associatedEntities) {
      PropertyAccessorHelper.set(child,reverseRelation.getProperty(),entity);
    }
  }
  if (associatedEntities != null) {
    MainCache mainCache=(MainCache)pd.getPersistenceCache().getMainCache();
    for (    Object child : associatedEntities) {
      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);
    }
  }
  if ((childMetadata.getRelationNames() == null || childMetadata.getRelationNames().isEmpty()) && !childMetadata.isRelationViaJoinTable()) {
  }
 else   if (associatedEntities != null) {
    for (    Object associatedEntity : associatedEntities) {
      associatedEntity=pd.getReader(childClient).recursivelyFindEntities(associatedEntity,null,childMetadata,pd);
    }
  }
}