NHibernate creates proxy via session.Load(), but not via Linq or Criteria API

197 views
Skip to first unread message

lcorneliussen

unread,
Oct 29, 2009, 10:22:49 AM10/29/09
to nhusers
Hi all together!

I'm sure I am missing something, but shouldn't each entity in a list,
when queried via the criteria API, be a proxy that enables lazy
loading?

I posted my question on stack overflow. The code is more readable over
there:

http://stackoverflow.com/questions/1643905/nhibernate-creates-proxy-via-session-load-but-not-via-linq-or-criteria-api

Any hints appreciated.

- Lars

Diego Mijelshon

unread,
Oct 29, 2009, 12:15:09 PM10/29/09
to nhu...@googlegroups.com
Short answer: no.

   Diego

Fabio Maulo

unread,
Oct 29, 2009, 12:39:32 PM10/29/09
to nhu...@googlegroups.com
lazy=extra and some specific indexed collection (for example <map> IDictionary).

2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>



--
Fabio Maulo

Diego Mijelshon

unread,
Oct 29, 2009, 2:11:01 PM10/29/09
to nhu...@googlegroups.com
You're correct... that's the long answer :-)

But unless you specify that, the answer to "shouldn't each entity in a list, when queried via the criteria API, be a proxy"  is no, as that's not the default behavior, nor the expected behavior with his mapping and query.

   Diego

PD: quedó largo el disclaimer, pero vos me entendés :-)

Fabio Maulo

unread,
Oct 29, 2009, 5:54:43 PM10/29/09
to nhu...@googlegroups.com
I never seen his mapping....

I can jump in another place to see an answer but not to read a question nor how recreate an issue.

2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>



--
Fabio Maulo

lars corneliussen

unread,
Oct 30, 2009, 9:09:55 AM10/30/09
to nhusers
hi,

the original problem was, that lazy loading didn't work. Instead for
each object found, all associations where load eagerly.

After quite some more research, I found the answers. Answers, because
there are many things that prevented lazy loading in NHibernate.

1. Query vs. session.Load: When fetching an item via session.Load()
you get a proxy. But as soon as you access any property, lets say the
Url, the object is fetched including all it's associations that
doesn't support lazy loading.

2. property-ref: Lazy loading only works over a objects id. When an
property-association is resolved via a different column in the target
entity, NH fetches it eagerly. Not that this wouldn't be possible,
it's just not implemented: http://nhjira.koah.net/secure/CreateIssue!default.jspa

3. not-found="ignore" allows invalid foreign keys, that is, if the
referenced entity isn't found NH will init the property with null. NH
doesn't intercept the property-access for lazy loading, but instead
assignes a object proxy. With not-found="ignore" it can't decide if
the property should be set to null or a proxy for the given, possibly
invalid, foreign key. This could possibly be solved by intercepting
the property access.

4. When disabling not-found="ignore" and property-ref the schema
export would generate constraints that enforce a circular reference.
Not good! The correct mapping would then be a constrained one-to-one
relationship, where the key for HippoAccountSync must have a generator
foreign.

Are these points right?

Could NH support lazy-loading for not-found="ignore" and property-ref
at some point, or is it impossible?

thanks!
- Lars

On 29 Okt., 22:54, Fabio Maulo <fabioma...@gmail.com> wrote:
> I never seen his mapping....
>
> I can jump in another place to see an answer but not to read a question nor
> how recreate an issue.
>
> 2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>
>
>
>
> > You're correct... that's the long answer :-)
>
> > But unless you specify that, the answer to "shouldn't each entity in a
> > list, when queried via the criteria API, be a proxy" is no, as that's not
> > the default behavior, nor the expected behavior with his mapping and query.
>
> > Diego
>
> > PD: quedó largo el disclaimer, pero vos me entendés :-)
>
> > On Thu, Oct 29, 2009 at 13:39, Fabio Maulo <fabioma...@gmail.com> wrote:
>
> >> lazy=extra and some specific indexed collection (for example <map>
> >> IDictionary).
>
> >> 2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>
>
> >> Short answer: no.
>
> >>> Diego
>
> >>> On Thu, Oct 29, 2009 at 11:22, lcorneliussen <l...@corneliussen.de>wrote:
>
> >>>> Hi all together!
>
> >>>> I'm sure I am missing something, but shouldn't each entity in a list,
> >>>> when queried via the criteria API, be a proxy that enables lazy
> >>>> loading?
>
> >>>> I posted my question on stack overflow. The code is more readable over
> >>>> there:
>
> >>>>http://stackoverflow.com/questions/1643905/nhibernate-creates-proxy-v...

lars corneliussen

unread,
Oct 30, 2009, 8:38:30 AM10/30/09
to nhusers
> I never seen his mapping....
>
> I can jump in another place to see an answer but not to read a question nor
> how recreate an issue.
>
> 2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>
>
>
>
> > You're correct... that's the long answer :-)
>
> > But unless you specify that, the answer to "shouldn't each entity in a
> > list, when queried via the criteria API, be a proxy"  is no, as that's not
> > the default behavior, nor the expected behavior with his mapping and query.
>
> >    Diego
>
> > PD: quedó largo el disclaimer, pero vos me entendés :-)
>
> > On Thu, Oct 29, 2009 at 13:39, Fabio Maulo <fabioma...@gmail.com> wrote:
>
> >> lazy=extra and some specific indexed collection (for example <map>
> >> IDictionary).
>
> >> 2009/10/29 Diego Mijelshon <di...@mijelshon.com.ar>
>
> >> Short answer: no.
>
> >>>    Diego
>
> >>> On Thu, Oct 29, 2009 at 11:22, lcorneliussen <l...@corneliussen.de>wrote:
>
> >>>> Hi all together!
>
> >>>> I'm sure I am missing something, but shouldn't each entity in a list,
> >>>> when queried via the criteria API, be a proxy that enables lazy
> >>>> loading?
>
> >>>> I posted my question on stack overflow. The code is more readable over
> >>>> there:
>
> >>>>http://stackoverflow.com/questions/1643905/nhibernate-creates-proxy-v...

Fabio Maulo

unread,
Oct 31, 2009, 9:31:45 AM10/31/09
to nhu...@googlegroups.com
which part ? the description or the solution of each ?
the desc. is right some solution is wrong.

2009/10/30 lars corneliussen <lcorne...@googlemail.com>



--
Fabio Maulo

lars corneliussen

unread,
Nov 2, 2009, 3:19:14 AM11/2/09
to nhusers
The real question is, if it would be possible to enable lazy loading
for not-found="ignore"? And if so, how much work would it be?

We have to find ways arround these problems in our current project.
Altering the NH-code could be one of them, if it is the easiest
solution.

We can get arround property-ref by changing the mapping.

- Lars

On 31 Okt., 14:31, Fabio Maulo <fabioma...@gmail.com> wrote:
> which part ? the description or the solution of each ?
> the desc. is right some solution is wrong.
>
> 2009/10/30 lars corneliussen <lcornelius...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages