aspose file tools
The moose likes Object Relational Mapping and the fly likes Reason for stack overflow exception while trying to read hibernate managed objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Reason for stack overflow exception while trying to read hibernate managed objects" Watch "Reason for stack overflow exception while trying to read hibernate managed objects" New topic
Author

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

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 457

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 ?


Regards
KumarRaja

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 18, 2010
Posts: 457

Yes, I can certainly persist the objects without explicit casting. That was an overlook from my side.

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 457

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reason for stack overflow exception while trying to read hibernate managed objects
 
Similar Threads
Hibernate example error message
org.hibernate.MappingException: table refers to an unmapped class
How to persist objects having circular references using Hibernate
getting same result in AutoWiring byType and byName
How to lazily load references when the object is detached from session ?