Lazy loading and long NH sessions

17 views
Skip to first unread message

FrenchC

unread,
Apr 25, 2008, 11:29:22 AM4/25/08
to nhusers
Hi all,
first of all, I'm a newbe to NH... Then: my problem is that I have to
entities: MupRuns and Mappings. The first one is the master entity and
the second one contains the children of each of the first one... When
I get access to the first one, if I set lazy="true" when I try to
dislpay the children of each of the master it return an error saying
that there is no session active...
To be precise consider that in the hbm.xml of the master the child is
setted as bag and in the master class the children list is setted as
an IList<Mappings>. Both these 2 entities aren't to be persisted on
the db...
Of course if I leave both 2 in lazy="false" all works fine, but the
performances decrease impressively...
Is there someone that can explain clearly and with some example how to
set a correct lazyloading?

Thx in advance
Francesco

Jason Meckley

unread,
Apr 25, 2008, 12:37:15 PM4/25/08
to nhusers
the session needs to remain open to use lazy loading.
this would break.
IFoo foo;
using (SessionFactory.CreateSession())
{
foo = session.Get<IFoo>(1)
}
foreach(IBar bar in Foo.Bars)
{
bar.DoSomething();
}

this would not
IFoo foo;
using (SessionFactory.CreateSession())
{
foo = session.Get<IFoo>(1)
foreach(IBar bar in Foo.Bars)
{
bar.DoSomething();

Sheraz

unread,
Apr 25, 2008, 12:40:06 PM4/25/08
to nhusers
Lazy loading means that you don't bring the associations when an
entity is loaded. But when you accessing the association (thru
properties), nHibernates goes and fetches if for you. In order for the
lazy loading to work, the session should be open. If it's not open
then you'd get that error. Also if you are using NH version 1.2 then
you don't need to set lazy=true as all associations are lazy by
default.
Example

ISession session = SessionFactory.OpenSession()
Master master = session.Load<Master>();//only master is loaded withoud
any associations
Children children = master.Children(); // lazily fetch children.
session.Close();

the children are fetched successfully as the session was open but if
you do this
ISession session = SessionFactory.OpenSession()
Master master = session.Load<Master>();//only master is loaded withoud
any associations
session.Close();
Children children = master.Children(); // lazily fetch children.

then you will get the error as the session is closed.
Hope that helps.


On Apr 25, 11:29 am, FrenchC <cres...@gmail.com> wrote:

Ian Joyce

unread,
Apr 25, 2008, 12:25:14 PM4/25/08
to nhu...@googlegroups.com
If your working in a web environment check out OpenSessionInView @
http://springframework.net/docs/1.1.1/reference/html/orm.html#orm-hibernate-web

--Ian

Sheraz

unread,
Apr 26, 2008, 6:24:08 PM4/26/08
to nhusers
if you working with web then the best thing to do is store session in
HttpContext and use HttModule and open the sesion on load and close on
unload.

On Apr 25, 12:25 pm, "Ian Joyce" <ian.jo...@gmail.com> wrote:
> If your working in a web environment check out OpenSessionInView @http://springframework.net/docs/1.1.1/reference/html/orm.html#orm-hib...
>
> --Ian
>
>
>
> On Fri, Apr 25, 2008 at 10:29 AM, FrenchC <cres...@gmail.com> wrote:
>
> >  Hi all,
> >  first of all, I'm a newbe to NH... Then: my problem is that I have to
> >  entities: MupRuns and Mappings. The first one is the master entity and
> >  the second one contains the children of each of the first one... When
> >  I get access to the first one, if I set lazy="true" when I try to
> >  dislpay the children of each of the master it return an error saying
> >  that there is no session active...
> >  To be precise consider that in the hbm.xml of the master the child is
> >  setted as bag and in the master class the children list is setted as
> >  an IList<Mappings>. Both these 2 entities aren't to be persisted on
> >  the db...
> >  Of course if I leave both 2 in lazy="false" all works fine, but the
> >  performances decrease impressively...
> >  Is there someone that can explain clearly and with some example how to
> >  set a correct lazyloading?
>
> >  Thx in advance
> >  Francesco- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages