-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to detach object from their Hibernate session ?
PostPosted: Tue May 06, 2008 6:52 am 
Newbie

Joined: Tue May 06, 2008 3:54 am
Posts: 6
Location: Bruxelles
Hi everybody,

I'm searching a mean (other thant creating DTO) to be able to detach an object from its hibernate session.

In fact, I retrieve an object list each of them containing objects sets and I'd like to force the initialization of those sets.

The object list is destinated to be serialized. When deserialized, the session is not available anymore.

I don't want to force the lazy to false in the hibernate mapping files beacause of the use of the service by others layers which may work with lazy loading.

I have already tried to use the following methods :

Code:
HibernateTemplate.initialize() [Spring]
-> "Force initialization of a Hibernate proxy or persistent collection."

Hibernate.initialize()
-> "provide the application with a convenient way of working with lazily initialized collections or proxies" ... "as long as its Session is still open"


But this give anything...

any help will be greatly appreciated !

Thanks to all,

Sebastien


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 4:57 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Both of those methods are fine. Of course you must do this within the context of the Session/Transaction that loaded the original entity. Do you see SQL being generated when you initialize the collection?

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 2:36 am 
Newbie

Joined: Tue May 06, 2008 3:54 am
Posts: 6
Location: Bruxelles
Thanks for your reply.

Yes, I'm well in the session that loaded the original entity. I don't see any sql generated when calling the initialize method...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 9:01 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Let's see the code, please. Note that if you load foo and want to initialize its bars collection, you should call Hibernate.initialize(foo.getBars()).

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject: This Jira helped me a lot on dis-assoc hibernate and POJO's
PostPosted: Wed May 07, 2008 9:31 am 
Beginner
Beginner

Joined: Fri May 18, 2007 10:28 am
Posts: 48
Location: Madison, WI
http://jira.codehaus.org/browse/XSTR-377

_________________
Please rate if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 10:34 am 
Newbie

Joined: Tue May 06, 2008 3:54 am
Posts: 6
Location: Bruxelles
Ok I have see my problem...

You are right Chris, it was my problem... I've to mention explicitely the collection I want to initialize.

But in my case it is very boring to do. Beacause my retrieved object contains set and the elements of this set contains also set,... So I have to itterate over each of those sets to force initialization....

I think the easier for me will be to create DTO finally.

Thanks all for your help.


Sébastien


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 10:39 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Consider using an HQL query with "join fetch" to tweak your fetch plan for each specific use case. Java Persistence with Hibernate talks a lot about this and is a very good read for this topic.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 11:02 am 
Newbie

Joined: Tue May 06, 2008 3:54 am
Posts: 6
Location: Bruxelles
Yes you are right !

Thanks again


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