11

If I save an object containing the following list

@OneToMany(cascade=CascadeType.ALL, mappedBy="taskList")
@OrderColumn(name="position", nullable=false)
public List<Task> tasks = new ArrayList<Task>();

I get the exception

org.hibernate.HibernateException: Found two representations of same collection

The code in the Play! controller looks like this:

TaskList taskList = taskList.findById(taskListId);
taskList.add(position, task);
taskList.save();

If I insert taskList.refresh() before this block it works, but the position information is lost (which leads to other errors).

Is this a Hibernate bug or is something wrong with my code?

deamon
  • 89,107
  • 111
  • 320
  • 448

7 Answers7

13

The problem was, that Hibernate does not support the combination of @OneToMany(mappedBy=...) and @OrderColumn. Without mappedBy Hibernate uses a join table and everything works as expected. See explanation.

Community
  • 1
  • 1
deamon
  • 89,107
  • 111
  • 320
  • 448
2

The same error occurs when you try to modify an associated collection of an object. e.g.:

    MyObject myObject = myObjectService.get(id);
    List<Task> newTasks = //populate new list of Task here
    myObject.setTasks(newTasks);
    myObjectService.saveOrUpdateObject(myObject); // or merge(myObject)      

In such a case, it can be resolved by:

    MyObject myObject = myObjectService.get(id);
    List<Task> newTasks = //populate new list of Task here
    myObject.setTasks(new List<Task>); // or myObject.getTasks().clear();
    myObject.getTasks().addAll(newTasks);
    myObjectService.merge(myObject); 
1

I could resolve the issue by changing the association to lazy and removing the cascade.

@OneToMany(mappedBy="taskList", fetch = FetchType.LAZY)
@OrderColumn(name="position", nullable=false)
public List<Task> tasks = new ArrayList<Task>();
Gat
  • 379
  • 5
  • 13
0

Another reason for the exception "org.hibernate.HibernateException: Found two representations of same collection" is duplicated getters/setters in the entity.

adelarsq
  • 3,718
  • 4
  • 37
  • 47
0

After a lot of hours I found the solution, I had the same problem.

I had an EntityListener with this code in my Spring Boot app

@PostLoad
public void defineRelation(EntityWithRelation entityWithRelation) {
        entityWithRelation.setRelation(objectRelationBo
                .findOneByObjectTypeAndReferenceId(entityWithRelation.getObject(), entityWithRelation.getId()));
}

This was causing the exception in a random (or looks like) collection of other unrelated entity that was participating in the same transaction. I don't know the exact reason but adding @Transactional(propagation = Propagation.REQUIRES_NEW) fixes the problem

Maybe Hibernate doesn't like you to modify the entity by hand, but the field was a transient field...

0

If you need the list to be persisted you might need to annotate your list with @OneToMany to be able to persist your entity.

emt14
  • 4,846
  • 7
  • 37
  • 58
-1

the problem is with your joining check whether you are not fatching any data which is not in the table