-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Fri Mar 26, 2010 8:59 am 
Newbie

Joined: Fri Mar 19, 2010 12:29 pm
Posts: 12
I have an entity class which contains field:
Code:
@ManyToOne(fetch = FetchType.LAZY)
private Department department;
This entity object was loaded in DAO class and session was closed. How to initialize now this LAZY field?
Only with usage of Query or Criteria for this entity, or there is other possibility to "EAGER" that lazy object, something like: session.[someMethod](department)?
I didn't find something like that in API, but I want to write some universal method for that in base HibernateDao class and inherit it in all DAO classes, something like that:
Code:
Object eagerObject (Object obj).
Is there any thoughts and ideas, how to do that?


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Fri Mar 26, 2010 9:18 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
org.hibernate.Hibernate.initialize() can take either a proxied entity or collection and initialize it.


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Fri Mar 26, 2010 9:47 am 
Newbie

Joined: Fri Mar 19, 2010 12:29 pm
Posts: 12
But in other session it doesn't work.


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Fri Mar 26, 2010 10:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need to attach it to an open session first. Use session.lock() (if you have not made any changes that needs to be persisted) or session.update() if there are changes.


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Tue Mar 30, 2010 3:47 am 
Newbie

Joined: Fri Mar 19, 2010 12:29 pm
Posts: 12
It works good with object. Thanks.

But how to do the same thing with collection?
Code:
@OneToMany(fetch = FetchType.LAZY,
mappedBy = "department",
cascade = {CascadeType.ALL})
List<DepartmentEmployee> employeers;


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Tue Mar 30, 2010 3:54 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have no such case myself, but try attaching the owner of the collection to an open session and then use Hibernate.initialize() with the collection.


Top
 Profile  
 
 Post subject: Re: How to "EAGER" previosly lazy loaded object in other session
PostPosted: Tue Mar 30, 2010 4:53 am 
Newbie

Joined: Fri Mar 19, 2010 12:29 pm
Posts: 12
When I use session.lock(object, LockMode.NONE) to the owner, all lazy collection of this object for some strange reasons initialized without Hibernate.initialize() usage. But I don't need all of them.


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