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:
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?
did you resoved it? I've got the same problem and haven't the solucion.
please, share it.
posted 5 years ago
@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.
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.