Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Hibernate bidirectional mapping working with annotations

 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Javi PerezFernandez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi J Ellis,

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

please, share it.

thank you!
 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Javi PerezFernandez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,



 
Amritendu De
author
Ranch Hand
Posts: 188
11
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Javi PerezFernandez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Krzysztof Mmm
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic