-->
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.  [ 10 posts ] 
Author Message
 Post subject: detach
PostPosted: Tue Jan 04, 2005 9:33 am 
Newbie

Joined: Tue Jan 04, 2005 9:30 am
Posts: 5
Is it possible to detach an object graph from a hibernate session in version 3.0. I had heard that this functionality had been/was going to be added.

In version 2 if you close a session and then start traversing the object graph you get the following:

ERROR - Failed to lazily initialize a collection - no session or session was closed

Is this fixed?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 9:35 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What makes you think that this is a bug? You have to read up on detachment/reattachment, there is plenty of material and forums postings about it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 1:21 pm 
Newbie

Joined: Tue Jan 04, 2005 9:30 am
Posts: 5
Fair enough. I was just wondering how to use the object after the session has been closed without getting an error. I haven't been able to find an answer in any of the other postings.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 1:58 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There are dozens, seriously. Just spend more than 10 minutes on this, its an important issue.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 5:42 pm 
Newbie

Joined: Tue Jan 04, 2005 9:30 am
Posts: 5
Sorry, I should've rephrased that, what I meant was I can find lots of postings about this problem but no useful answers.

One posting suggests that the session must be kept open while you traverse the object graph. How would this work if you've just sent the object to a tier with no access to the database.

Another posting suggests that you must fully populate the object graph before closing the session. This seems too inefficient to be practical.

Another posting suggests that I should copy the hibernate proxied object to another non-hibernate proxied object before I transfer it across tiers. I suspect I'd have to do this manually as I'm guessing a reflective "automagic" copier would trigger Hibernate to lazily load. This seems like a lot of effort.

Basically I just want to load an object and then ask hibernate to let go of it, so I can manipulate it manually. Hibernate doesn't seem to like "sharing" though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 5:49 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
pants wrote:
Sorry, I should've rephrased that, what I meant was I can find lots of postings about this problem but no useful answers.

One posting suggests that the session must be kept open while you traverse the object graph. How would this work if you've just sent the object to a tier with no access to the database.


The session only needs to be open if the object graph has no lazily loaded collections or proxies.

pants wrote:
Another posting suggests that you must fully populate the object graph before closing the session. This seems too inefficient to be practical.


pants wrote:
Another posting suggests that I should copy the hibernate proxied object to another non-hibernate proxied object before I transfer it across tiers. I suspect I'd have to do this manually as I'm guessing a reflective "automagic" copier would trigger Hibernate to lazily load. This seems like a lot of effort.


This is acutally pretty normal in an EJB application using Hibernate. DTOs are created from the PDOs and returned to the EJB clients. It's actually quite nice to have different DTOs for different purposes.

pants wrote:
Basically I just want to load an object and then ask hibernate to let go of it, so I can manipulate it manually. Hibernate doesn't seem to like "sharing" though.


Okay, here are some suggestions:
1) Instead of using 'detach', make deep clone methods for your PDOs and then manipulate the clones.
2) Use the 'constructor' feature in Hibernate3 (this might not work for collections though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 6:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh, i'm not sure DO or other DTO are usefull with POJOs, they are usefull with _dependant_ business objectsl ike ejb1 or 2, please forget it for webapps.

Two years ago my team was working with DTO and copyProperty stuff , this doesn't work when you have complex object network, you have 20 lines of copyproperty in your controller layer in each method in relation to view layer... without any business meaning... waste of time.

Then we have coded a DTO factory and realized (we were helped by hibernate friends ;)) that using pojo directly in the jsp was very easy with open session in view.

So detached instance works very well. But when a modified instance come back to server, you have to play with update(), lock() method and other select-before-update parameter.... this is not hard but long session is much much easier if you know how to use it, i love it!

Long-session-per-application-transaction isn't only usefull for lazy initilization, it's really a great strategy for use case that needs more than one view to do some some.

Open session in view solves 95% of lazy initialization problems in web applications.

Are you coding a rich client or is your app a webapp?

And if i can give an advice, use long session, it works great but you must define your use case very well, it is not intented that the session have a _too_ long life.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 6:23 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Finally, if you really want to understand more about your options, buy the ebook of Hibernate in Action.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Guys, you both are right.

Sometimes a DTO is the right anser. Sometimes detachement and reattachement of business POJOs. Sometimes a long Session.

The pro and con of this has been discussed a dozen times. There is also a good explanation of the issues in Hibernate in Action as well as in several conference presentations we did (the slides are usually available, some on our website).

I don't want to repeat all of this here, its a lot of material.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 5:11 am 
Newbie

Joined: Tue Jan 04, 2005 9:30 am
Posts: 5
The app I'm coding is a "middle tier" supporting a variety of clients - web apps, web services, rich clients, messaging brokers.

It seems that the only solution is to create copies (clones) of proxied hibernate objects. Bit of a pain, would have been nice if I could just "detach" the object.

I'll check out the book though, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.