Setting FetchMode on a grandchild object

7 views
Skip to first unread message

Mike

unread,
Jul 6, 2010, 2:46:04 PM7/6/10
to nhusers
I have the following Criteria query (using Lambda extensions):

var workflowResult = repository.GetSession()
.CreateCriteria<Core.Domain.Application>()
.SetFetchMode<Core.Domain.Application>(app =>
app.ApplicationWorkflows, FetchMode.Join)
.Add(SqlExpression.In<Core.Domain.Application>(app =>
app.ID, uniqueIds))
.SetResultTransformer(new
DistinctRootEntityResultTransformer())
.Future<Core.Domain.Application>();

This is working correctly. Each Application eagerly loads the
ApplicationWorkflows collection. However, I'd like to go one deeper
and load the ApplicationStatus object of each ApplicationWorkflow. I
can do this with the following HQL but would like to translate to
Criteria:

var workflowQuery = "SELECT DISTINCT app" +
" FROM Application app" +
" JOIN FETCH app.ApplicationWorkflows
awf" +
" JOIN FETCH awf.ApplicationStatus" +
" WHERE app.ID IN (:appList2)";

Thanks!

Richard Brown (gmail)

unread,
Jul 6, 2010, 5:12:57 PM7/6/10
to nhusers
IIRC you can write:

.CreateCriteria<Core.Domain.Application>()
.SetFetchMode<Core.Domain.Application>(app => app.ApplicationWorkflows,

FetchMode.Eager)
.SetFetchMode<Core.Domain.Application>(app =>
app.ApplicationWorkflows[0].ApplicationStatus, FetchMode.Eager)
.Add(SqlExpression.In<Core.Domain.Application>(app =>...

Also remember to consider performance:
http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-aggregate-with-many-child-collections.aspx

--------------------------------------------------
From: "Mike" <cole...@gmail.com>
Sent: Tuesday, July 06, 2010 7:46 PM
To: "nhusers" <nhu...@googlegroups.com>
Subject: [nhusers] Setting FetchMode on a grandchild object

> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

Mike

unread,
Jul 6, 2010, 5:36:29 PM7/6/10
to nhusers
That did the trick... thanks!

And thanks for the note on performance. I have been working on
optimizing my old queries that I did before NHProf.

On Jul 6, 4:12 pm, "Richard Brown \(gmail\)" <fluke...@googlemail.com>
wrote:
> IIRC you can write:
>
>     .CreateCriteria<Core.Domain.Application>()
>     .SetFetchMode<Core.Domain.Application>(app => app.ApplicationWorkflows,
> FetchMode.Eager)
>     .SetFetchMode<Core.Domain.Application>(app =>
> app.ApplicationWorkflows[0].ApplicationStatus, FetchMode.Eager)
>     .Add(SqlExpression.In<Core.Domain.Application>(app =>...
>
> Also remember to consider performance:http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-...
>
> --------------------------------------------------
> From: "Mike" <cole.m...@gmail.com>

Mike

unread,
Jul 6, 2010, 5:38:20 PM7/6/10
to nhusers
As an side, can you give me an example of creating a Cartesian
product? I've heard the reference several times and I think I
understand what it means, but just want some confirmation. Thanks!

On Jul 6, 4:12 pm, "Richard Brown \(gmail\)" <fluke...@googlemail.com>
wrote:
> IIRC you can write:
>
>     .CreateCriteria<Core.Domain.Application>()
>     .SetFetchMode<Core.Domain.Application>(app => app.ApplicationWorkflows,
> FetchMode.Eager)
>     .SetFetchMode<Core.Domain.Application>(app =>
> app.ApplicationWorkflows[0].ApplicationStatus, FetchMode.Eager)
>     .Add(SqlExpression.In<Core.Domain.Application>(app =>...
>
> Also remember to consider performance:http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-...
>
> --------------------------------------------------
> From: "Mike" <cole.m...@gmail.com>

Mike

unread,
Aug 30, 2010, 11:46:09 AM8/30/10
to nhusers
Hello,
How would I perform this using simple criteria API with no lamdba
extensions? Thanks!

On Jul 6, 4:12 pm, "Richard Brown \(gmail\)" <fluke...@googlemail.com>
wrote:
> IIRC you can write:
>
>     .CreateCriteria<Core.Domain.Application>()
>     .SetFetchMode<Core.Domain.Application>(app => app.ApplicationWorkflows,FetchMode.Eager)
>     .SetFetchMode<Core.Domain.Application>(app =>
> app.ApplicationWorkflows[0].ApplicationStatus,FetchMode.Eager)
>     .Add(SqlExpression.In<Core.Domain.Application>(app =>...
>
> Also remember to consider performance:http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-...
>
> --------------------------------------------------
> From: "Mike" <cole.m...@gmail.com>
> Sent: Tuesday, July 06, 2010 7:46 PM
> To: "nhusers" <nhu...@googlegroups.com>
> Subject: [nhusers]SettingFetchModeon agrandchildobject
>
>
>
> > I have the following Criteria query (using Lambda extensions):
>
> > var workflowResult = repository.GetSession()
> >                .CreateCriteria<Core.Domain.Application>()
> >                .SetFetchMode<Core.Domain.Application>(app =>
> > app.ApplicationWorkflows,FetchMode.Join)
> >                .Add(SqlExpression.In<Core.Domain.Application>(app =>
> > app.ID, uniqueIds))
> >                .SetResultTransformer(new
> > DistinctRootEntityResultTransformer())
> >                .Future<Core.Domain.Application>();
>
> > This is working correctly.  Each Application eagerly loads the
> > ApplicationWorkflows collection.  However, I'd like to go one deeper
> > and load the ApplicationStatusobjectof each ApplicationWorkflow.  I

Richard Brown (gmail)

unread,
Sep 1, 2010, 8:25:26 AM9/1/10
to nhusers
I think it's:

.CreateCriteria<Core.Domain.Application>()
.SetFetchMode("ApplicationWorkflows" ,FetchMode.Eager)
.SetFetchMode("ApplicationWorkflows.ApplicationStatus",
FetchMode.Eager)
...


--------------------------------------------------
From: "Mike" <cole...@gmail.com>
Sent: Monday, August 30, 2010 4:46 PM
To: "nhusers" <nhu...@googlegroups.com>
Subject: [nhusers] Re: Setting FetchMode on a grandchild object

Reply all
Reply to author
Forward
0 new messages