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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
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: 522
    
    2

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: 522
    
    2

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: 522
    
    2

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://aspose.com/file-tools
 
subject: Reason for stack overflow exception while trying to read hibernate managed objects