Over a million developers have joined DZone.

Hibernate Facts: Favoring bidirectional Set(s) vs List(s)

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Coucbase.


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, which is very well described here.

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.

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:

@Entity
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) {
   children.add(child);
   child.setParent(this);
}

public void removeChild(Child child) {
   children.remove(child);
   child.setParent(null);
}
}

@Entity
public class Child {

@ManyToOne
private Parent parent;

public Parent getParent() {
return parent;
}

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

Parent parent = loadParent(parentId);
Child child1 = new Child();
child1.setName("child1");
Child child2 = new Child();
child2.setName("child2");
parent.addChild(child1);
parent.addChild(child2);
entityManager.merge(parent);


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-3332  and HHH-5855.

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 parent0_.id as id1_2_0_ from Parent parent0_ where parent0_.id=?
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, the my 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:

@Entity
public class LinkedParent {

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

public Set getChildren() {
   return children;
}

public void addChild(LinkedChild child) {
   children.add(child);
   child.setParent(this);
}

public void removeChild(LinkedChild child) {
   children.remove(child);
   child.setParent(null);
}
}


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

select 
children0_.parent_id as parent_i3_3_1_, 
children0_.id as id1_2_1_, 
children0_.id as id1_2_0_, 
children0_.name as name2_2_0_, 
children0_.parent_id as parent_i3_2_0_ 
from LinkedChild children0_ 
where children0_.parent_id=? order by children0_.id


Source code:

https://github.com/vladmihalcea/vladmihalcea.wordpress.com/tree/master/hibernate-facts

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:
java ,sql ,hibernate ,performance ,tips and tricks

Published at DZone with permission of Vlad Mihalcea, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}