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…
View original post 605 more words