Program: 176767
Project: 176767
Buggy source:
/** 
 * Recursively fetches associated entities for a given <code>entity</code>
 * @param entity
 * @param relationsMap
 * @param client
 * @param m
 * @param pd
 * @return
 */
public Object recursivelyFindEntities(Object entity,Map<String,Object> relationsMap,EntityMetadata m,PersistenceDelegator pd){
  Object entityId=PropertyAccessorHelper.getId(entity,m);
  associationBuilder=new AssociationBuilder();
  for (  Relation relation : m.getRelations()) {
    Field f=relation.getProperty();
    Object object=PropertyAccessorHelper.getObject(entity,f);
    if (object == null || object instanceof HibernateProxy || object instanceof PersistentSet || object instanceof PersistentCollection) {
      FetchType fetch=relation.getFetchType();
      if (relation.isRelatedViaJoinTable()) {
        PersistenceCacheManager.addEntityToPersistenceCache(entity,pd,entityId);
        associationBuilder.populateRelationFromJoinTable(entity,m,pd,relation);
      }
 else {
        String relationName=MetadataUtils.getMappedName(m,relation);
        Object relationValue=relationsMap != null ? relationsMap.get(relationName) : null;
        Class<?> childClass=relation.getTargetEntity();
        EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(childClass);
        if (relationValue != null) {
          associationBuilder.populateRelationFromValue(entity,pd,relation,relationValue,childMetadata);
        }
 else {
          associationBuilder.populateRelationViaQuery(entity,pd,entityId,relation,relationName,childMetadata);
        }
      }
    }
  }
  return entity;
}

Fixed version (ground truth):
/** 
 * Recursively fetches associated entities for a given <code>entity</code>
 * @param entity
 * @param relationsMap
 * @param client
 * @param m
 * @param pd
 * @return
 */
public Object recursivelyFindEntities(Object entity,Map<String,Object> relationsMap,EntityMetadata m,PersistenceDelegator pd){
  Object entityId=PropertyAccessorHelper.getId(entity,m);
  associationBuilder=new AssociationBuilder();
  for (  Relation relation : m.getRelations()) {
    ForeignKey type=relation.getType();
    if (isTraversalRequired(relationsMap,type)) {
      Field f=relation.getProperty();
      Object object=PropertyAccessorHelper.getObject(entity,f);
      if (object == null || object instanceof HibernateProxy || object instanceof PersistentSet || object instanceof PersistentCollection) {
        FetchType fetch=relation.getFetchType();
        if (relation.isRelatedViaJoinTable()) {
          PersistenceCacheManager.addEntityToPersistenceCache(entity,pd,entityId);
          associationBuilder.populateRelationFromJoinTable(entity,m,pd,relation);
        }
 else {
          String relationName=MetadataUtils.getMappedName(m,relation);
          Object relationValue=relationsMap != null ? relationsMap.get(relationName) : null;
          Class<?> childClass=relation.getTargetEntity();
          EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(childClass);
          if (relationValue != null) {
            associationBuilder.populateRelationFromValue(entity,pd,relation,relationValue,childMetadata);
          }
 else {
            associationBuilder.populateRelationViaQuery(entity,pd,entityId,relation,relationName,childMetadata);
          }
        }
      }
    }
  }
  return entity;
}