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.  [ 2 posts ] 
Author Message
 Post subject: Help: don't understand lazy.
PostPosted: Thu Sep 14, 2006 9:28 pm 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi,

I'm using NHibernate and C#. I've problem using the 'lazy' directive. When I try to access a set (in the case below, is the parentDocs of the Document class) which has lazy set to true, I hit error (the error line is on the om.parentDocs.Count) stating that system use lazy instantiation since there is no session available. As such, I've added line to open/get the session again (see=NHibernateSessionUtil.Session will give open session). But this doesn't help.

Hope you guides tell me which area I should take note/modify when I set to 'lazy'? Or my code can't call like this (i.e. om.parentDocs.Count is invalid) or I need to explicitly call a HQL query to get the count result?

thanks in advance,
jL

code below:
public ArrayList getRootParentDocs(Document om)
{
ArrayList alResult = new ArrayList();
Document doc = new Document();
sess = NHibernateSessionUtil.Session;
ITransaction tx = sess.BeginTransaction();

if (om.parentDocs.Count == 0)
{
alResult.Add(om);
tx.Commit();
sess.Close();
}
else {
for (IEnumerator ie = om.parentDocs.GetEnumerator(); ie.MoveNext(); )
{
doc = (Document)ie.Current;
getRootParentDocs(doc);
}
sess.Close();
}
return alResult;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 9:46 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
You have to keep the same session open in order to use lazy loading. Usually to achieve this people implement some kind of Unit of Work pattern to cleanly know when to flush and close the session.

In a web app, typically this is done in the begin/end request. Begin request opens the session, end request flushes and closes it. Anything in between gets the same session, so lazy loading works fine.

HTH. Lazy loading is a tough subject to learn in NHib.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


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