-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy Loading after closing the session
PostPosted: Fri Jan 16, 2009 3:18 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
Hi,
my question is very simple.
below is my method.

public EmployeeDTO getEmployeeInfo( String userId ) throws PersistenceCriticalException {
EmployeeDTO employeeDTO = null;
try {
begin();
employeeDTO = ( EmployeeDTO ) getSession().get(EmployeeDTO.class, new Integer(userId));

commit();
} catch( Exception e ) {
throw new PersistenceCriticalException( e );
} finally {
close();
}
return employeeDTO;
}

after closing the session by close(); method.
i cant retrieve the employeeDTO's objects lazily.
if i set lazy="false" in mapping file I can view retrieve the data.

however, setting lazy="false" is hampering my performance.
is there any other way to retrieve the required information lazily after closing the session();

or is there any other method where i dont need to compromise on performance?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 3:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll always need an open session. So if the original session has been closed you have to open a new session and use Session.lock() to reassociate the original object with the new session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:28 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
Thanks buddy :)
but how can i carry my session object to UI or Domain layer. it wont be a good practice.

Ne ways, thanks for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is a common problem and there are many different solutions to it. The main problem here is that your DAO is closing the session. This is usually considered an anti-pattern. Opening and closing sessions should usually be handled at some higher level that knows about the current "unit-of-work".

To begin with you should read http://www.hibernate.org/43.html

A lot of Hibernate users are using Spring which has builtin support for session handling. I have no experience with Spring myself, but it seems to be popular when using Hibernate in a web environment.


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