Lazy loading a property

2 views
Skip to first unread message

bdaniel7

unread,
Oct 21, 2009, 3:10:38 AM10/21/09
to Castle Project Users
Hello,

I have this situation:
http://dblendea.pastebin.com/f52096576

News has some properties and also Content, which is quite large and I
want it loaded lazily.
I tried to use the example provided by Gabriel Schenker here:
http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/17/lazy-loading-blobs-and-the-like-in-nhibernate.aspx

The hbm-s generated look like his, so I'm assuming I've made it
correctly.

But the test shows 3 statements instead of one.
Title is not lazy, but Content is.

1. SELECT top 2 this_.Id as Id5_0_, this_.Title as Title5_0_,
this_.Content as Content5_0_ FROM News this_ WHERE this_.Title like
@p0;@p0 = '%Error in NHibernate config!%'
2. SELECT newsconten0_.Id as Id4_0_, newsconten0_.Text as Text4_0_
FROM NewsContents newsconten0_ WHERE newsconten0_.Id=@p0;@p0 = 1
3. SELECT news0_.Id as Id5_0_, news0_.Title as Title5_0_,
news0_.Content as Content5_0_ FROM News news0_ WHERE
news0_.Content=@p0;@p0 = 1

Did I do something wrong or is it another way of loading a property
lazily?

Thanks,
Dan

bdaniel7

unread,
Oct 21, 2009, 3:09:48 PM10/21/09
to Castle Project Users
Anybody...?

On Oct 21, 10:10 am, bdaniel7 <bdani...@gmail.com> wrote:
> Hello,
>
> I have this situation:http://dblendea.pastebin.com/f52096576
>
> News has some properties and also Content, which is quite large and I
> want it loaded lazily.
> I tried to use the example provided by Gabriel Schenker here:http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/17/lazy...

bdaniel7

unread,
Nov 5, 2009, 11:01:19 AM11/5/09
to Castle Project Users
still nobody?
is my question really that dumb? or that complex?

On Oct 21, 9:09 pm, bdaniel7 <bdani...@gmail.com> wrote:
> Anybody...?
>
> On Oct 21, 10:10 am, bdaniel7 <bdani...@gmail.com> wrote:
>
> > Hello,
>
> > I have this situation:http://dblendea.pastebin.com/f52096576
>
> > News has some properties and also Content, which is quite large and I
> > want it loaded lazily.
> > I tried to use the example provided by Gabriel Schenker here:http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/17/lazy...
>
> > The hbm-s generated look like his, so I'm assuming I've made it
> > correctly.
>
> > But the test shows 3 statements instead of one.
> > Title is notlazy, but Content is.

Mauricio Scheffer

unread,
Nov 5, 2009, 11:07:05 AM11/5/09
to Castle Project Users
Try asking on nhusers: http://groups.google.com/group/nhusers

bdaniel7

unread,
Nov 5, 2009, 11:41:56 AM11/5/09
to Castle Project Users
I will ask there too, but I'm using AR to load the objects.

On Nov 5, 6:07 pm, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:

Ricardo Lopes

unread,
Nov 5, 2009, 1:16:15 PM11/5/09
to castle-pro...@googlegroups.com

The NHibernate 2.1 is comparable to Hibernate 3.2.6 so it should works, I just don't know how. 

I doubt there is support for this using ActiveRecord, the alternative is to use a projection in HQL or using a criteria.

Hope that helps.

2009/11/5 bdaniel7 <bdan...@gmail.com>



--

Ricardo Lopes

Fábio Batista

unread,
Nov 5, 2009, 1:34:52 PM11/5/09
to castle-pro...@googlegroups.com
Here is what I usually do:

http://dblendea.pastebin.com/f2c192a48

There's a little more code, but it's simple enough and makes you feel
in control.

Fábio David Batista
fabio.dav...@gmail.com
http://nerd-o-matic.blogspot.com

Fábio Batista

unread,
Nov 5, 2009, 1:37:12 PM11/5/09
to castle-pro...@googlegroups.com
Tip: you'll need to save your News entity prior to calling
CreateContent(), or else you'll not get the correct Id. I usually
throw an exception in case I try to call CreateContent() and the Id ==
0.

I'm sure there's a more elegant way out there, though...
2009/11/5 Fábio Batista <fabio.dav...@gmail.com>:

Roelof Blom

unread,
Nov 5, 2009, 3:59:12 PM11/5/09
to castle-pro...@googlegroups.com
How about this?

[ActiveRecord( SelectBeforeUpdate=true, DynamicInsert = true, DynamicUpdate = true)]
public class News : BusinessObject<News>
{
    // all properties here, except content

   
    [Lazy=FetchWhen.OnInvoke]
    public NewsContent Content { get;set;}
}

2009/11/5 Fábio Batista <fabio.dav...@gmail.com>

bdaniel7

unread,
Nov 6, 2009, 7:52:16 AM11/6/09
to Castle Project Users
This is how I did it a while ago, while exploring the options from AR,
but the test shows the same number of selects.
Probably I'll go with the Fábio's solution.

On Nov 5, 10:59 pm, Roelof Blom <roelof.b...@gmail.com> wrote:
> How about this?
>
> [ActiveRecord( SelectBeforeUpdate=true, DynamicInsert = true, DynamicUpdate
> = true)]
> public class News : BusinessObject<News>
> {
>     // all properties here, except content
>
>     *[Lazy=FetchWhen.OnInvoke]*
>     public NewsContent Content { get;set;}
>
> }
>
> 2009/11/5 Fábio Batista <fabio.david.bati...@gmail.com>
>
>
>
> > Tip: you'll need to save your News entity prior to calling
> > CreateContent(), or else you'll not get the correct Id. I usually
> > throw an exception in case I try to call CreateContent() and the Id ==
> > 0.
>
> > I'm sure there's a more elegant way out there, though...
>
> > Fábio David Batista
> > fabio.david.bati...@gmail.com
> >http://nerd-o-matic.blogspot.com
>
> > 2009/11/5 Fábio Batista <fabio.david.bati...@gmail.com>:
> > > Here is what I usually do:
>
> > >http://dblendea.pastebin.com/f2c192a48
>
> > > There's a little more code, but it's simple enough and makes you feel
> > > in control.
>
> > > Fábio David Batista
> > > fabio.david.bati...@gmail.com
> > >http://nerd-o-matic.blogspot.com
>
> > > On Thu, Nov 5, 2009 at 4:16 PM, Ricardo Lopes <rjlo...@gmail.com> wrote:
>
> >http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performanc...
> > >> The NHibernate 2.1 is comparable to Hibernate 3.2.6 so it should works,
> > I
> > >> just don't know how.
> > >> I doubt there is support for this using ActiveRecord, the alternative is
> > to
> > >> use a projection in HQL or using a criteria.
> > >> Hope that helps.
> > >> 2009/11/5 bdaniel7 <bdani...@gmail.com>
Reply all
Reply to author
Forward
0 new messages