Program: 176430
Project: 176430
Buggy source:
/** 
 * Populates a a relationship collection which is of type   {@link Map} fromrelationsMap into entity
 * @param entity
 * @param delegator
 * @param relation
 * @param relObject
 * @param relationsMap
 */
private void populateCollectionFromMap(Object entity,PersistenceDelegator delegator,Relation relation,Object relObject,Map<String,Object> relationsMap){
  EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
  Map<Object,Object> relationshipEntityMap=new HashMap<Object,Object>();
  if (relObject == null && relationsMap != null && !relationsMap.isEmpty()) {
    for (    String relationName : relationsMap.keySet()) {
      Object relationValue=relationsMap.get(relationName);
      if (relationValue instanceof Map) {
        Map<Object,Object> relationValueMap=(Map<Object,Object>)relationValue;
        Client targetEntityClient=delegator.getClient(childMetadata);
        for (        Object targetEntityKey : relationValueMap.keySet()) {
          Object targetEntity=targetEntityClient.find(childMetadata.getEntityClazz(),targetEntityKey);
          if (targetEntity != null && targetEntity instanceof EnhanceEntity) {
            targetEntity=((EnhanceEntity)targetEntity).getEntity();
          }
          Object mapKeyEntity=relationValueMap.get(targetEntityKey);
          Class<?> relationshipClass=relation.getMapKeyJoinClass();
          for (          Field f : relationshipClass.getDeclaredFields()) {
            if (f.getType().equals(entity.getClass())) {
              PropertyAccessorHelper.set(mapKeyEntity,f,entity);
            }
 else             if (f.getType().equals(childMetadata.getEntityClazz())) {
              PropertyAccessorHelper.set(mapKeyEntity,f,targetEntity);
            }
          }
          relationshipEntityMap.put(mapKeyEntity,targetEntity);
        }
      }
    }
    relObject=relationshipEntityMap;
  }
  PropertyAccessorHelper.set(entity,relation.getProperty(),relObject);
  if (relObject != null) {
    for (    Object child : ((Map)relObject).values()) {
      if (child != null) {
        Object childId=PropertyAccessorHelper.getId(child,childMetadata);
        PersistenceCacheManager.addEntityToPersistenceCache(child,delegator,childId);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Populates a a relationship collection which is of type   {@link Map} fromrelationsMap into entity
 * @param entity
 * @param delegator
 * @param relation
 * @param relObject
 * @param relationsMap
 */
private void populateCollectionFromMap(Object entity,PersistenceDelegator delegator,Relation relation,Object relObject,Map<String,Object> relationsMap){
  EntityMetadata childMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
  Map<Object,Object> relationshipEntityMap=new HashMap<Object,Object>();
  if ((relObject == null || ProxyHelper.isProxyCollection(relObject)) && relationsMap != null && !relationsMap.isEmpty()) {
    for (    String relationName : relationsMap.keySet()) {
      Object relationValue=relationsMap.get(relationName);
      if (relationValue instanceof Map) {
        Map<Object,Object> relationValueMap=(Map<Object,Object>)relationValue;
        Client targetEntityClient=delegator.getClient(childMetadata);
        for (        Object targetEntityKey : relationValueMap.keySet()) {
          Object targetEntity=targetEntityClient.find(childMetadata.getEntityClazz(),targetEntityKey);
          if (targetEntity != null && targetEntity instanceof EnhanceEntity) {
            targetEntity=((EnhanceEntity)targetEntity).getEntity();
          }
          Object mapKeyEntity=relationValueMap.get(targetEntityKey);
          Class<?> relationshipClass=relation.getMapKeyJoinClass();
          for (          Field f : relationshipClass.getDeclaredFields()) {
            if (f.getType().equals(entity.getClass())) {
              PropertyAccessorHelper.set(mapKeyEntity,f,entity);
            }
 else             if (f.getType().equals(childMetadata.getEntityClazz())) {
              PropertyAccessorHelper.set(mapKeyEntity,f,targetEntity);
            }
          }
          relationshipEntityMap.put(mapKeyEntity,targetEntity);
        }
      }
    }
    relObject=relationshipEntityMap;
  }
  PropertyAccessorHelper.set(entity,relation.getProperty(),relObject);
  if (relObject != null && !ProxyHelper.isProxyCollection(relObject)) {
    for (    Object child : ((Map)relObject).values()) {
      if (child != null) {
        Object childId=PropertyAccessorHelper.getId(child,childMetadata);
        PersistenceCacheManager.addEntityToPersistenceCache(child,delegator,childId);
      }
    }
  }
}