How do Set and List collections behave with JPA and Hibernate


Hibernate is a great ORM tool, and it eases development considerably, but it has a lot of gotchas you must be aware of if you want to use it properly.

On medium to large projects, it’s very common to have bidirectional parent-child associations, which allow us to navigate both ends of a given relationship.

When it comes to controlling the persist/merge part of the association, there are two options available. One would be to have the @OneToMany end in charge of synchronizing the collection changes, but this is an inefficient approach.

The most common approach is when the @ManyToOne side controls the association and the @OneToMany end is using the “mappedBy” option.

I will discuss the latter approach since it’s the most common and the most efficient one, in terms of the executed queries number.

Bidirectional associations

So, for bidirectional collections, we could use a java.util.List or a java.util.Set.

According to Hibernate docs, lists and bags are more efficient than sets.

But I am still getting anxious when I see the following code:

public class Parent {

    @OneToMany(cascade = CascadeType.ALL, 
        mappedBy = "parent", orphanRemoval = true)
    private List children = new ArrayList()

    public List getChildren() {
        return children;

    public void addChild(Child child) {

    public void removeChild(Child child) {

public class Child {

    private Parent parent;

    public Parent getParent() {
        return parent;

    public void setParent(Parent parent) {
        this.parent = parent;

Parent parent = loadParent(parentId);
Child child1 = new Child();
Child child2 = new Child();

This is because for the last five years I’ve been getting duplicate children inserted when the merge operation is called on the parent association. This happens because of the following issues HHH-5855.

The HHH-5855 issue was fixed in Hibernate 5.0.8, so another reason to update.

I’ve been testing some Hibernate versions lately and this still replicates on 3.5.6, 3.6.10 and 4.2.6 versions. So, after 5 years of seeing this on many projects you understand why I’m being skeptical of using Lists vs Sets.

This is what I get when running a test case replicating this issue, so for adding two children we get:

select as id1_2_0_ from Parent parent0_ where
insert into Child (id, name, parent_id) values (default, ?, ?)
insert into Child (id, name, parent_id) values (default, ?, ?)
insert into Child (id, name, parent_id) values (default, ?, ?)
insert into Child (id, name, parent_id) values (default, ?, ?)

This issue only replicates if a merge operations is cascaded from parent to children, and there are workarounds like:

  • merging the child instead of the parent
  • persisting the children prior to merging the parent
  • removing the Cascade.ALL or Cascade.MERGE from parent, since it only affects the merge operation and not the persist one.

But all of those are hacks, and are very difficult to follow on a large-scale project, with many developers working on the same code base.

So, until HHH-5855 is fixed, the preferred way is to use Sets, even if sometimes they’ll be less efficient than Lists, but since I always favor correctness vs performance optimization, I am better off using Sets.

When it comes to this types of problems, it’s good to have code conventions, as they are easy to add in a project development guideline, and are also easier to remember and be adopted.

One advantage of using Sets is that it forces you to define a proper equals/hashCode strategy (which should always include the entity’s business key. A business key is a field combinations that’s unique, or unique among a parent’s children, and that’s consistent even before and after the entity is persisted into the database).

If you are worried you are going to lose the List ability of saving the children in the same order you’ve added them, then you can still emulate this for Sets too.

By default Sets are unordered and unsorted, but even if you can’t order them you may still sort them by a given column, by using the @OrderBy JPA annotation like this:

public class LinkedParent {

    @OneToMany(cascade = CascadeType.ALL, 
        mappedBy = "parent", orphanRemoval = true)
    private Set children = new LinkedHashSet();

    public Set getChildren() {
        return children;

    public void addChild(LinkedChild child) {

    public void removeChild(LinkedChild child) {

When the parent’s children are loaded, the generated SQL is like:

   children0_.parent_id as parent_i3_3_1_, as id1_2_1_, as id1_2_0_, as name2_2_0_,
   children0_.parent_id as parent_i3_2_0_ 
   LinkedChild children0_ 
order by

If you enjoyed this article, I bet you are going to love my book as well.


If your domain model requires using a List than a Set will break your constraint, disallowing duplicates. But if you need duplicates you can still use an Indexed List. A Bag is said to be unsorted and “unordered” (even if it retrieves the children in the order they were added in the database table). So an indexed List would be also a good candidate, right?

I also wanted to draw attention on a 5 year bug, affecting multiple Hibernate versions, and one that I replicated on multiple projects. There are workarounds of course, like removing the Cascade.Merge or merging the Children vs the Parent, but there are many developers unaware of this issue and its workarounds.

According to Hibernate docs: Sets are “the recommended way to represent many-valued associations“, and I’ve seen many cases where Bags were employed as the default bi-directional collection, even if a set would have been a better choice anyway.

Code available on GitHub.

Enter your email address to follow this blog and receive notifications of new posts by email.


7 thoughts on “How do Set and List collections behave with JPA and Hibernate

  1. I’ve always used Lists instead of Sets and I just got this issue (replicated children) when I used bidirectional associations. And this issue just happened once!

    By the way, great post!

  2. I had an onetomany relation and entity on many(child table) side have an @ElementCollection. When i use to fetch the Many side entity from the one side entity, it started to dubplicate the result. It took a while to realize the problem and after changing onetomany relation to Set, i wondered why this happened in first case. You got nice explanation. Thank you

    1. @ElementCollection is not the same with a one-to-many association. An @ElementCollection is used to associate an Entity with a collection of Embeddable components. The Embeddable components life-cycle is bound to their Parent entity, and you can’t query Embeddable, like you’d query Entities.

  3. Hello Vlad,
    The first thing if I need help for JPA I google for “”.

    If I use Set instead of List. What is the preferred way so put the TypedQuery.getResultList() into a List?


    Or is there a way of receiving a Set from the EntityManager?

    Kind Regards,

    1. Thanks. When you execute a Query, you always get a List. Why do you want to populate a Set instead? Also, the bug described here has been fixed in 5.2, so you should be fine using Lists instead of Sets.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s