Program: 125726
Project: 125726
Buggy source:
public void saveRelations(){
  if (!loaded && !modified) {
    return;
  }
  Project currentProject=getAimProject();
  currentProject.getEmfProject().getItemsRelations().clear();
  for (  Relation relation : currentProjectItemsRelations.keySet()) {
    ItemRelations 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) {
      ItemRelation emfRelatedItem=PropertiesFactory.eINSTANCE.createItemRelation();
      emfRelatedItem.setId(relatedItem.getId());
      emfRelatedItem.setType(relatedItem.getType());
      emfRelatedItem.setVersion(relatedItem.getVersion());
      itemRelations.getRelatedItems().add(emfRelatedItem);
    }
    currentProject.getEmfProject().getItemsRelations().add(itemRelations);
  }
  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) {
      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().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;
}