aspose file tools*
The moose likes Object Relational Mapping and the fly likes Getting Hibernate bidirectional mapping working with annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Getting Hibernate bidirectional mapping working with annotations" Watch "Getting Hibernate bidirectional mapping working with annotations" New topic
Author

Getting Hibernate bidirectional mapping working with annotations

J Ellis
Greenhorn

Joined: Aug 23, 2009
Posts: 28
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

Joined: Nov 14, 2014
Posts: 3
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

Joined: Aug 23, 2009
Posts: 28
@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

Joined: Nov 14, 2014
Posts: 3
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

Joined: Feb 21, 2009
Posts: 145
    
    6

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


Senior Architect (SCEA 5, TOGAF 9, IBM OOAD, PMI certified)
[Spring, Hibernate, Data Modeling, REST and TDD:Agile Java Design and Development] [OCMJEA Practice Guide] [OCA Java SE-7 and SE-6 Practice Exams]
Javi PerezFernandez
Greenhorn

Joined: Nov 14, 2014
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Hibernate bidirectional mapping working with annotations