Program: 176090
Project: 176090
Buggy source:
/** 
 * Populates a relationship of type   {@link Collection} (i.e. those of type{@link Set} or {@link List})
 */
private void populateCollectionFromJoinTable(Object entity,EntityMetadata entityMetadata,PersistenceDelegator delegator,Relation relation){
  JoinTableMetadata jtMetadata=relation.getJoinTableMetadata();
  Client pClient=delegator.getClient(entityMetadata);
  String schema=entityMetadata.getSchema();
  EntityMetadata owningEntityMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
  Class columnJavaType=owningEntityMetadata.getIdAttribute().getJavaType();
  if (jtMetadata == null) {
    columnJavaType=entityMetadata.getIdAttribute().getJavaType();
    jtMetadata=owningEntityMetadata.getRelation(relation.getMappedBy()).getJoinTableMetadata();
    pClient=delegator.getClient(owningEntityMetadata);
    schema=owningEntityMetadata.getSchema();
  }
  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 entityId=PropertyAccessorHelper.getId(entity,entityMetadata);
  List<?> foreignKeys=pClient.getColumnsById(schema,joinTableName,joinColumnName,inverseJoinColumnName,entityId,columnJavaType);
  List childrenEntities=new ArrayList();
  if (foreignKeys != null) {
    for (    Object foreignKey : foreignKeys) {
      EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
      Object child=delegator.find(relation.getTargetEntity(),foreignKey);
      Object obj=child instanceof EnhanceEntity && child != null ? ((EnhanceEntity)child).getEntity() : child;
      Field biDirectionalField=relation.getBiDirectionalField();
      boolean isBidirectionalRelation=(biDirectionalField != null);
      if (isBidirectionalRelation && obj != null) {
        Object columnValue=PropertyAccessorHelper.getId(obj,childMetadata);
        Object[] pKeys=pClient.findIdsByColumn(entityMetadata.getSchema(),joinTableName,joinColumnName,inverseJoinColumnName,columnValue,entityMetadata.getEntityClazz());
        List parents=delegator.find(entity.getClass(),pKeys);
        PropertyAccessorHelper.set(obj,biDirectionalField,ObjectUtils.getFieldInstance(parents,biDirectionalField));
      }
      childrenEntities.add(obj);
    }
  }
  Field childField=relation.getProperty();
  try {
    PropertyAccessorHelper.set(entity,childField,PropertyAccessorHelper.isCollection(childField.getType()) ? ObjectUtils.getFieldInstance(childrenEntities,childField) : childrenEntities.get(0));
    PersistenceCacheManager.addEntityToPersistenceCache(entity,delegator,entityId);
  }
 catch (  PropertyAccessException ex) {
    throw new EntityReaderException(ex);
  }
}

Fixed version (ground truth):
/** 
 * Populates a relationship of type   {@link Collection} (i.e. those of type{@link Set} or {@link List})
 */
private void populateCollectionFromJoinTable(Object entity,EntityMetadata entityMetadata,PersistenceDelegator delegator,Relation relation){
  JoinTableMetadata jtMetadata=relation.getJoinTableMetadata();
  Client pClient=delegator.getClient(entityMetadata);
  String schema=entityMetadata.getSchema();
  EntityMetadata owningEntityMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
  Class columnJavaType=owningEntityMetadata.getIdAttribute().getJavaType();
  if (jtMetadata == null) {
    columnJavaType=entityMetadata.getIdAttribute().getJavaType();
    jtMetadata=owningEntityMetadata.getRelation(relation.getMappedBy()).getJoinTableMetadata();
    pClient=delegator.getClient(owningEntityMetadata);
    schema=owningEntityMetadata.getSchema();
  }
  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 entityId=PropertyAccessorHelper.getId(entity,entityMetadata);
  List<?> foreignKeys=pClient.getColumnsById(schema,joinTableName,joinColumnName,inverseJoinColumnName,entityId,columnJavaType);
  List childrenEntities=new ArrayList();
  if (foreignKeys != null) {
    for (    Object foreignKey : foreignKeys) {
      EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
      Object child=delegator.find(relation.getTargetEntity(),foreignKey);
      Object obj=child instanceof EnhanceEntity && child != null ? ((EnhanceEntity)child).getEntity() : child;
      Field biDirectionalField=relation.getBiDirectionalField();
      boolean isBidirectionalRelation=(biDirectionalField != null);
      if (isBidirectionalRelation && obj != null) {
        Object columnValue=PropertyAccessorHelper.getId(obj,childMetadata);
        Object[] pKeys=pClient.findIdsByColumn(entityMetadata.getSchema(),joinTableName,joinColumnName,inverseJoinColumnName,columnValue,entityMetadata.getEntityClazz());
        List parents=delegator.find(entity.getClass(),pKeys);
        PropertyAccessorHelper.set(obj,biDirectionalField,ObjectUtils.getFieldInstance(parents,biDirectionalField));
        PersistenceCacheManager.addEntityToPersistenceCache(obj,delegator,columnValue);
      }
      childrenEntities.add(obj);
    }
  }
  Field childField=relation.getProperty();
  try {
    PropertyAccessorHelper.set(entity,childField,PropertyAccessorHelper.isCollection(childField.getType()) ? ObjectUtils.getFieldInstance(childrenEntities,childField) : childrenEntities.get(0));
    PersistenceCacheManager.addEntityToPersistenceCache(entity,delegator,entityId);
  }
 catch (  PropertyAccessException ex) {
    throw new EntityReaderException(ex);
  }
}