Program: 176061
Project: 176061
Buggy source:
/** 
 * Parse relations of provided relationEntity.
 * @param originalEntity
 * @param relationEntity
 * @param relationsMap
 * @param pd
 * @param metadata
 * @param lazilyloaded
 */
private void parseRelations(final Object originalEntity,final Object relationEntity,final Map<String,Object> relationsMap,final PersistenceDelegator pd,final EntityMetadata metadata,boolean lazilyloaded){
  for (  Relation relation : metadata.getRelations()) {
    FetchType fetchType=relation.getFetchType();
    if (!lazilyloaded && fetchType.equals(FetchType.LAZY)) {
      final Object entityId=PropertyAccessorHelper.getId(relationEntity,metadata);
      getAssociationBuilder().setProxyRelationObject(relationEntity,relationsMap,metadata,pd,entityId,relation);
    }
 else {
      if (relation.isUnary() && relation.getTargetEntity().isAssignableFrom(originalEntity.getClass())) {
        Object associationObject=PropertyAccessorHelper.getObject(relationEntity,relation.getProperty());
        if (relation.getType().equals(ForeignKey.ONE_TO_ONE)) {
          if ((associationObject == null || ProxyHelper.isProxyOrCollection(associationObject))) {
            PropertyAccessorHelper.set(relationEntity,relation.getProperty(),originalEntity);
          }
        }
 else         if (relationsMap != null && relationsMap.containsKey(relation.getJoinColumnName())) {
          PropertyAccessorHelper.set(relationEntity,relation.getProperty(),originalEntity);
        }
      }
 else {
        final Object entityId=PropertyAccessorHelper.getId(relationEntity,metadata);
        Object relationValue=relationsMap != null ? relationsMap.get(relation.getJoinColumnName()) : null;
        final EntityMetadata targetEntityMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
        List immediateRelations=fetchRelations(relation,metadata,pd,entityId,relationValue,targetEntityMetadata);
        if (immediateRelations != null && !immediateRelations.isEmpty()) {
          for (          Object immediateRelation : immediateRelations) {
            if (!compareTo(getEntity(immediateRelation),originalEntity)) {
              onParseRelation(relationEntity,pd,targetEntityMetadata,immediateRelation,relation,lazilyloaded);
            }
          }
          setRelationToEntity(relationEntity,originalEntity,relation);
          PersistenceCacheManager.addEntityToPersistenceCache(getEntity(relationEntity),pd,PropertyAccessorHelper.getId(relationEntity,metadata));
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Parse relations of provided relationEntity.
 * @param originalEntity
 * @param relationEntity
 * @param relationsMap
 * @param pd
 * @param metadata
 * @param lazilyloaded
 */
private void parseRelations(final Object originalEntity,final Object relationEntity,final Map<String,Object> relationsMap,final PersistenceDelegator pd,final EntityMetadata metadata,boolean lazilyloaded){
  for (  Relation relation : metadata.getRelations()) {
    FetchType fetchType=relation.getFetchType();
    if (!lazilyloaded && fetchType.equals(FetchType.LAZY)) {
      final Object entityId=PropertyAccessorHelper.getId(relationEntity,metadata);
      getAssociationBuilder().setProxyRelationObject(relationEntity,relationsMap,metadata,pd,entityId,relation);
    }
 else {
      if (relation.isUnary() && relation.getTargetEntity().isAssignableFrom(originalEntity.getClass())) {
        Object associationObject=PropertyAccessorHelper.getObject(relationEntity,relation.getProperty());
        if (relation.getType().equals(ForeignKey.ONE_TO_ONE)) {
          if ((associationObject == null || ProxyHelper.isProxyOrCollection(associationObject))) {
            PropertyAccessorHelper.set(relationEntity,relation.getProperty(),originalEntity);
          }
        }
 else         if (relationsMap != null && relationsMap.containsKey(relation.getJoinColumnName())) {
          PropertyAccessorHelper.set(relationEntity,relation.getProperty(),originalEntity);
        }
      }
 else {
        final Object entityId=PropertyAccessorHelper.getId(relationEntity,metadata);
        Object relationValue=relationsMap != null ? relationsMap.get(relation.getJoinColumnName()) : null;
        final EntityMetadata targetEntityMetadata=KunderaMetadataManager.getEntityMetadata(relation.getTargetEntity());
        List immediateRelations=fetchRelations(relation,metadata,pd,entityId,relationValue,targetEntityMetadata);
        if (immediateRelations != null && !immediateRelations.isEmpty()) {
          for (          Object immediateRelation : immediateRelations) {
            if (immediateRelation != null && !compareTo(getEntity(immediateRelation),originalEntity)) {
              onParseRelation(relationEntity,pd,targetEntityMetadata,immediateRelation,relation,lazilyloaded);
            }
          }
          setRelationToEntity(relationEntity,originalEntity,relation);
          PersistenceCacheManager.addEntityToPersistenceCache(getEntity(relationEntity),pd,PropertyAccessorHelper.getId(relationEntity,metadata));
        }
      }
    }
  }
}