-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate addToXXX lazy initialization problem in 3 tier app
PostPosted: Mon Aug 23, 2004 1:16 am 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
Hi guys,
I am developing a 3 tier app (struts, ejb, hibernate). now the problem is that i have a parent - child relationship where i retrieve the parent object in the ejb and transfer that object to struts, now when i am in struts, i create many child objects and want to add to these child objects to the parent object so i call the parent.addTo(child) but i got a lazy initialization exception. what i had planned to do is to add the children to the parent and only transfer the parent only back to the ejb to update and save the parent and the children into the database. can anyone tell me is there anyway to tell hibernate that the parent is a detached object and not try to do anything to it? Thanks to anyone in advance who can help me out on this one!!


Best regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 2:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
add to these child objects to the parent object

this operation shouldn't be in the ui layer...
anyway, if you want to avoid the exception just fetch the collection when querying

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 6:22 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
anthony wrote:
Quote:
add to these child objects to the parent object

this operation shouldn't be in the ui layer...
anyway, if you want to avoid the exception just fetch the collection when querying


This is only part of the solution. You may hit the same problem while accessing your lazy loaded properties in your ui layer (which is valid). In this situation, forcing the 2nd tier to initialize all properties that will be used by the ui layer is not something that should be part of the 2nd tier (or maybe in a kind of adapter layer in between).

Having tried different solutions, it seems to me the best approach is to have to Hibernate session handling made by a servlet filter. This way, you are sure your Session is opened until the request is completed - even during JSP rendering if any.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 2:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
bertrand,
of course your approach "protects" you from the lazy__exception but is only ok for webapps or apps where the ui is rendered in the same server side as the session.
When you have rich client on a remote _hardware_, this isn't so easy.lol


And remember that servletFilter protects you from this exception but it doesn't optimise the graph loading.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 3:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
In rich clients this can get ugly quickly - for example if you have a JTree structure you want to lazy-load. I implemented something like that by constantly sending the objects back to the server and then doing session.lock(), Hibernate.initialize() on lazy objects. However this can get messy and you have to know what you are doing.

In the meantime, I tend to think that you are better off to create a seperate data model structure for such things, where you copy the values over from the persistent Objects.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.