@OneToMany List vs Set difference

A list, if there is no index column specified, will just be handled as a bag by Hibernate (no specific ordering).

One notable difference in the handling of Hibernate is that you can’t fetch two different lists in a single query. For example, if you have a Person entity having a list of contacts and a list of addresses, you won’t be able to use a single query to load persons with all their contacts and all their addresses. The solution in this case is to make two queries (which avoids the cartesian product), or to use a Set instead of a List for at least one of the collections.

It’s often hard to use Sets with Hibernate when you have to define equals and hashCode on the entities and don’t have an immutable functional key in the entity.

Leave a Comment