Program: 125485
Project: 125485
Buggy source:
public void saveRelations(){
  if (!loaded && !modified) {
    return;
  }
  Project currentProject=getAimProject();
  List<ItemRelations> oldRelations=new ArrayList<ItemRelations>(currentProject.getEmfProject().getItemsRelations());
  List<ItemRelations> usedList=new ArrayList<ItemRelations>();
  for (  Relation relation : currentProjectItemsRelations.keySet()) {
    ItemRelations itemRelations=null;
    boolean exist=false;
    for (    ItemRelations relations : oldRelations) {
      if (relations.getBaseItem().getId().equals(relation.getId())) {
        usedList.add(relations);
        itemRelations=relations;
        exist=true;
        break;
      }
    }
    if (itemRelations == null) {
      itemRelations=PropertiesFactory.eINSTANCE.createItemRelations();
      ItemRelation baseItem=PropertiesFactory.eINSTANCE.createItemRelation();
      itemRelations.setBaseItem(baseItem);
      baseItem.setId(relation.getId());
      baseItem.setType(relation.getType());
      baseItem.setVersion(relation.getVersion());
    }
    List<Relation> relationItemsList=new ArrayList<Relation>(currentProjectItemsRelations.get(relation));
    Collections.sort(relationItemsList,new Comparator<Relation>(){
      @Override public int compare(      Relation o1,      Relation o2){
        return o1.getType().compareTo(o2.getType());
      }
    }
);
    for (    Relation relatedItem : relationItemsList) {
      List<ItemRelation> relationList=new ArrayList<ItemRelation>(itemRelations.getRelatedItems());
      boolean found=false;
      for (      ItemRelation item : relationList) {
        if (item.getId() != null && item.getId().equals(relatedItem.getId())) {
          found=true;
          break;
        }
      }
      if (found) {
        continue;
      }
      ItemRelation emfRelatedItem=PropertiesFactory.eINSTANCE.createItemRelation();
      emfRelatedItem.setId(relatedItem.getId());
      emfRelatedItem.setType(relatedItem.getType());
      emfRelatedItem.setVersion(relatedItem.getVersion());
      itemRelations.getRelatedItems().add(emfRelatedItem);
    }
    if (!exist) {
      currentProject.getEmfProject().getItemsRelations().add(itemRelations);
    }
  }
  oldRelations.removeAll(usedList);
  currentProject.getEmfProject().getItemsRelations().removeAll(oldRelations);
  try {
    getProxyRepositoryFactory().saveProject(currentProject);
  }
 catch (  PersistenceException e) {
    ExceptionHandler.process(e);
  }
  modified=false;
}

Fixed version (ground truth):
public void saveRelations(){
  if (!loaded && !modified) {
    return;
  }
  Project currentProject=getAimProject();
  List<ItemRelations> oldRelations=new ArrayList<ItemRelations>(currentProject.getEmfProject().getItemsRelations());
  List<ItemRelations> usedList=new ArrayList<ItemRelations>();
  for (  Relation relation : currentProjectItemsRelations.keySet()) {
    ItemRelations itemRelations=null;
    boolean exist=false;
    for (    ItemRelations relations : oldRelations) {
      boolean isIdSame=relations.getBaseItem().getId().equals(relation.getId());
      boolean isVersionSame=StringUtils.equals(relations.getBaseItem().getVersion(),relation.getVersion());
      if (isIdSame && isVersionSame) {
        usedList.add(relations);
        itemRelations=relations;
        exist=true;
        break;
      }
    }
    if (itemRelations == null) {
      itemRelations=PropertiesFactory.eINSTANCE.createItemRelations();
      ItemRelation baseItem=PropertiesFactory.eINSTANCE.createItemRelation();
      itemRelations.setBaseItem(baseItem);
      baseItem.setId(relation.getId());
      baseItem.setType(relation.getType());
      baseItem.setVersion(relation.getVersion());
    }
    List<Relation> relationItemsList=new ArrayList<Relation>(currentProjectItemsRelations.get(relation));
    Collections.sort(relationItemsList,new Comparator<Relation>(){
      @Override public int compare(      Relation o1,      Relation o2){
        return o1.getType().compareTo(o2.getType());
      }
    }
);
    for (    Relation relatedItem : relationItemsList) {
      List<ItemRelation> relationList=new ArrayList<ItemRelation>(itemRelations.getRelatedItems());
      boolean found=false;
      for (      ItemRelation item : relationList) {
        boolean isIdSame=false;
        String itemId=item.getId();
        if (itemId != null) {
          isIdSame=itemId.equals(relatedItem.getId());
        }
        boolean isVersionSame=false;
        if (isIdSame) {
          isVersionSame=StringUtils.equals(item.getVersion(),relatedItem.getVersion());
        }
        if (isIdSame && isVersionSame) {
          found=true;
          break;
        }
      }
      if (found) {
        continue;
      }
      ItemRelation emfRelatedItem=PropertiesFactory.eINSTANCE.createItemRelation();
      emfRelatedItem.setId(relatedItem.getId());
      emfRelatedItem.setType(relatedItem.getType());
      emfRelatedItem.setVersion(relatedItem.getVersion());
      itemRelations.getRelatedItems().add(emfRelatedItem);
    }
    if (!exist) {
      currentProject.getEmfProject().getItemsRelations().add(itemRelations);
    }
  }
  oldRelations.removeAll(usedList);
  currentProject.getEmfProject().getItemsRelations().removeAll(oldRelations);
  try {
    getProxyRepositoryFactory().saveProject(currentProject);
  }
 catch (  PersistenceException e) {
    ExceptionHandler.process(e);
  }
  modified=false;
}