File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reason for stack overflow exception while trying to read hibernate managed objects

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm writing some sample codes in hibernate to understand the association and just started with One-One mapping. I ran into an issue where it says



Below are my codes

Address.java


Student.java


Address.hbm.xml


Student.hbm.xml


The reason which I'm thinking of here is when we say getStudent(), toString() method of Student class invoked as expected and in toString(), we have a reference of Address and it would invoke toString() method defined in Address class. This is getting into the infamous vicious circle of circular reference and finally throwing the error. I'm almost certain on this, as when I print only student name, it is working fine. But it is throwing error, when I try to refer Address from Student object.

My questions
1) Is my understanding of the error correct ? Or is there something else that could have caused the issue (which I'm certainly not hoping for).
2) Are there any hidden caveats in this code, which I should be aware of ?
3) How can this be better designed ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand why you're doing this--can't you just save an object without casting it? The save method, after all, doesn't have specific save methods for each class.

But yes, you're in an infinite loop.

And please don't try to include BB Code markup in source code--surely you can see it didn't have the intended effect.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I can certainly persist the objects without explicit casting. That was an overlook from my side.

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But some how I'm thinking that this bidirectional association is not correctly designed and there is a high risk of circular dependencies and references. Is there any good approach/industry standard that can be suggested espicially while handling bidirectional assocation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic