wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate StackOverflowError: bidirectional many-to-many Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate StackOverflowError: bidirectional many-to-many" Watch "Hibernate StackOverflowError: bidirectional many-to-many" New topic
Author

Hibernate StackOverflowError: bidirectional many-to-many

Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
I have two objects to persist; Account and Role. Account has a String username, a String password and a Set of Roles. Role has a String name, and a Set of Accounts. Hibernate creates a join table called accountroles to manage the Sets' many-to-many relationship. Everything works fine; Hibernate generates the correct SQL as far as I can tell. I inserted intermediate commits at each step just to make sure. On the last commit, I get a java.lang.StackOverflowError. I assume this has something to do with the bidirectional nature of the relationship. I must be getting an infinite loop of recursion or something. Any idea what I did wrong?

Thanks!

Hibernate version:
version 3.0.2 , 27.04.2005

Config file:



Mapping documents:
Account.hbm.xml


Role.hbm.xml



Code between sessionFactory.openSession() and session.close():



Schema-export.sql:



Full stack trace of any exception that occurs:
java.lang.StackOverflowError (that's it)


Name and version of the database you are using:
hsqldb_1_7_3_3


The generated SQL (show_sql=true):


Debug level Hibernate log excerpt:
Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
I almost forgot to mention, the above code uses the same instance of HashSet for userRole and adminRole.

The following code gave me the exact same result:


I also tried out this project with MySQL 3.0.16 and had the same problem.

In case anyone wants to try this out, I've published the project at:
SimpleHibernateDemo.zip
Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
I don't know why my last reply shows up blank, but I also wanted to mention:

The above code uses the same instance of HashSet for userRole and adminRole. I also tried this:



but I got the same problem. I also tried out this project with MySQL 3.0.16. Same issue. In case anyone wants to try this out, I've published the project at:

http://java.briankuhn.com/SimpleHibernateDemo.zip
Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
I don't know why my last 2 replies show up blank, but I still want to mention:

The above code uses the same instance of HashSet for userRole and adminRole. I also tried this:



but I got the same problem. I also tried out this project with MySQL 3.0.16. Same issue. In case anyone wants to try this out, I've published the project at:

http://java.briankuhn.com/SimpleHibernateDemo.zip
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate StackOverflowError: bidirectional many-to-many
 
Similar Threads
No error/exception in log on insert yet no row inserted to db
Hibernate3.0 with Weblogic7.0
problem with hibernate mapping file
insert/update value of the FOREIGN KEY is not equal to any value of parent key of parent table:DB2
a question about hibernate + JBX