I have a many-to-many relationship were the link table has an additional field. Hence the relationship is done with 2 one-to-many relations according to below tutorial:
http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/comment-page-1/#comment-122181
I have the 2 entities, a third entity which defined the link table and consist of an @Embeddable ID field.
The relationship is defined as:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.compound", cascade = CascadeType.ALL)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.structure", cascade = CascadeType.ALL)
were pk = the @Embeddable ID field. Insert and deleting works fine however when I call
session.merge(compound);
I get a StackOverflowError and log shows that hibernate is making tons of select statements. Note that database contains exactly 1 association, eg. 1 compound containing 2 structures. It looks like hibernate gets into a endless loop.
I've seen this solution also on http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/ but how do you do updated with this?
My solution was to use FetctType.EAGER on the owning side and FetchType.Lazy on child side and in the 2 ManyToOne relations in the link table. Like this I can load from the owning side without getting LazyInitializationException and mergign working as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With