Forums Register Login

Getting Hibernate bidirectional mapping working with annotations

+Pie Number of slices to send: Send
Hi all,

I have a very simple database with two tables: Departments [Id, Name] and Employees [Id, DepartmentsId, FirstName, LastName]. I am trying to get a bidirectional relationship working in Hibernate but it keeps generating an infinite loop and then a StackOverFlow exception. Here are my classes:

Departments:



Employees:



My HQL query is simply: "FROM Departments d JOIN FETCH d.employeeses e".

I have tried Departments using both the existing and commented-out sets of annotations on "getEmployeeses". According to everything I have been reading, including the Hibernate documentation on bidirectional collections (http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html#collections-bidirectional) these annotations should, in effect, set the "inverse" types for the two tables and thus avoid the problem I am running into, that every time "Departments" grabs an "Employees" set, each new "Employees" object creates another "Departments," which then creates another "Employees" set, so on and so forth, into an infinite loop. However, the issue still occurs unless I remove the "Departments" object from the "Employees" class, thus creating a unidirectional mapping, which I do not want to do. What the heck am I doing wrong here?
+Pie Number of slices to send: Send
Hi J Ellis,

did you resoved it? I've got the same problem and haven't the solucion.

please, share it.

thank you!
+Pie Number of slices to send: Send
@Javi: This was quite some time ago so I do not remember the exact outcome, but I believe I never did quite solve it. I eventually ended up moving away from Hibernate altogether because it just turned out to be too heavyweight and clunky for me (this is not to say that Hibernate is not a great tool, just that I myself am not great at implementing it). I ended up moving to other frameworks and just writing and manipulating queries and data transfer objects myself.

I did ask the same question on StackOverflow and a user there said that in general you run into this kind of thing when you serialize your entities. He recommended manually transferring data to DTOs and serializing those instead. There was some discussion there but no one ever came up with a good example of that in action. Since this was two years ago I imagine that there is probably a lot more information and examples out there now then there were back then.

Here is the StackOverflow link:

http://stackoverflow.com/questions/10264143/how-to-avoid-endless-loop-in-hibernate-when-fetching-bidirectional-collections

Sorry I couldn't be of more help.
+Pie Number of slices to send: Send
Hi J Ellis,

Thank you for your answer. I have been trying some "solutions" that, like you and other users explain on this forum or stackoverflow ,but i'm sure that i'm doing something wrong because it doesn't work....yet.

I hope find the solution soon.

Regards,



+Pie Number of slices to send: Send
My upcoming book has an entire chapter on one to many bidirectional relationship. You can see it here: http://amritendude.blogspot.in/2014/06/new-book-on-spring-4-and-hibernate-4.html
+Pie Number of slices to send: Send
Hi all,

I have found the problem.

If you use lombok api, it has incompatibilities between hibernate and lombok. I tested annotations like and but donn't work.

The solution for me is don't use annotation lombok for model hibernate and use tipical constructor objects and getters/setters

Good luck for all.

Regards,

javierpsf
+Pie Number of slices to send: Send
Thank you Javi, you saved my day.
I can also add that I've replaced Lombok's @Data with @Setter and @Getter and it started to work as it should.
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5958 times.
Similar Threads
problem with @FieldResult, mapping does not seem to work
Simple requirement .. but I don't know how!
One to many Relation
Simple requirement .. but I don't know how!
Unable to understand exception
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:09:10.