-->
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.  [ 9 posts ] 
Author Message
 Post subject: Lazy Loading Example (in Web App) ?
PostPosted: Mon Feb 02, 2004 2:24 am 
Newbie

Joined: Wed Jan 28, 2004 9:04 pm
Posts: 7
Is there a good example of using lazy loading in a web app somewhere?

Essentially what I want to do is (using an example with single User has multiple contacts): find a User object without loading the contacts collection and put it into the HttpSession. Then later (in another request) get the User object from the HttpSession and get the contacts for the User.
It sounds simple however all my attempts so far have resulted in a Could not lazily load collection - no session.

Thanks,
Jerry.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 2:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
use session.lock(user, LockMode.NONE) in the second request


Top
 Profile  
 
 Post subject: Lazy Loading Example (in Web App) ?
PostPosted: Mon Feb 02, 2004 6:33 pm 
Newbie

Joined: Wed Jan 28, 2004 9:04 pm
Posts: 7
I tried your suggestion in my code, but I still get the same error. First I am trying to lazily load the contacts in the same request. In the struts action class I get a single User and then try to get the contacts for that user in another call to the DBService class.

The second call has the session.lock(user, LockMode.NONE) code in it.

Jerry.



List userList = DBService.getInstance().getUserList();

User user = (User)userList.get(0);

Set contacts = DBService.getInstance().getUserContacts(user);




public class DBService
{

private static DBService instance = null;

private DBService()
{

}


public static synchronized DBService getInstance()
{
if (instance == null)
{
instance = new DBService();
}
return instance;
}


public List getUserList()
{
Session session = ConnectionFactory.getInstance().getSession();

try
{
Query query =
session.createQuery(
"select user from User user");

System.out.println("size is: " + query.list().size());

return query.list();

}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}

}
}
}


public Set getUserContacts(User user)
{
Session session = ConnectionFactory.getInstance().getSession();

try
{
session.lock(user, LockMode.NONE);
return user.getContacts();

}
catch (Exception e)
{

throw new RuntimeException(e);
}

finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}

}
}
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 6:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you using a recent hibernate version?


Top
 Profile  
 
 Post subject: Lazy Loading Example (in Web App) ?
PostPosted: Mon Feb 02, 2004 7:05 pm 
Newbie

Joined: Wed Jan 28, 2004 9:04 pm
Posts: 7
Yes I am using the latest version of Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I am pretty sure reassociating works with recent Hibernate, I am using this myself all the time. You must be missing something.


Top
 Profile  
 
 Post subject: Lazy Loading Example (in Web App) ?
PostPosted: Mon Feb 02, 2004 7:17 pm 
Newbie

Joined: Wed Jan 28, 2004 9:04 pm
Posts: 7
Would you have a code sample I can take a look at?

Thanks,
Jerry. (jerry_s220@yahoo.com)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 7:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I am pretty sure your code should work, mine looks very similar, I even pass the objects around to a remote app-server before reattatching. Is what you have shown really exactly the stuff you do?


Top
 Profile  
 
 Post subject: Lazy Loading Example (in Web App) ?
PostPosted: Tue Feb 03, 2004 2:34 am 
Newbie

Joined: Wed Jan 28, 2004 9:04 pm
Posts: 7
I think I have it working now. After I added this extra line after session.lock(user, LockMode.NONE) it seems to work fine:

Hibernate.initialize(user.getContacts());

I hope this will be useful for the other users to know..

Jerry.


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