eager fetching over multiple levels, that's more than 2 :-)

61 views
Skip to first unread message

mhnyborg

unread,
Aug 10, 2009, 4:21:16 PM8/10/09
to nhusers
I just want to here if NH 2.1 has some new features that makes
selection from 3 levels possible.

for example I want to load all customers with all orders and order
lines. I can get the Customers and orders using
Transformers.DistinctRootEntity but that's only fixes the customer
collection I get to many orders.

The problem is better explained here:
http://stackoverflow.com/questions/332703/nhibernate-eager-fetching-over-multiple-levels


Fabio Maulo

unread,
Aug 10, 2009, 5:08:45 PM8/10/09
to nhu...@googlegroups.com
which is the question ?

2009/8/10 mhnyborg <mhny...@gmail.com>



--
Fabio Maulo

mhnyborg

unread,
Aug 10, 2009, 5:25:46 PM8/10/09
to nhusers
The question is: can I distinct load this object graph: Baselines-
HasMany-BaselineMilestones-HasMany-BaselineMilestonePrevious

var baselines = session.CreateQuery(" from Baseline b left join fetch
b.BaselineMilestones bm left join fetch bm.BaselineMilestonePrevious
")
.SetResultTransformer
(Transformers.DistinctRootEntity)
.List<Baseline>();

Assert.IsNotNull(baselines,"baselines is not null"); WORKS
Assert.AreEqual(39, baselines.Count(), "Baseline.Count() == 39");
WORKS
Assert.AreEqual(baselines[0].BaselineMilestones.Count, 56); TO MANY
RETURNED

The problem is that I need a DistinctRootEntity logic to be applied to
BaselineMilestones

On Aug 10, 11:08 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> which is the question ?
>
> 2009/8/10 mhnyborg <mhnyb...@gmail.com>
>
>
>
>
>
> > I just want to here if NH 2.1 has some new features that makes
> > selection from 3 levels possible.
>
> > for example I want to load all customers with all orders and order
> > lines. I can get the Customers and orders using
> > Transformers.DistinctRootEntity but that's only fixes the customer
> > collection I get to many orders.
>
> > The problem is better explained here:
>
> >http://stackoverflow.com/questions/332703/nhibernate-eager-fetching-o...
>
> --
> Fabio Maulo

Dario Quintana

unread,
Aug 10, 2009, 5:56:14 PM8/10/09
to nhu...@googlegroups.com
If your question is how to do it, here you have an example:

select o from Order o join fetch o.Lines li join fetch li.Article where o.Id = :Id

OR

s.CreateCriteria(typeof (Order))
                    .SetFetchMode("Lines", FetchMode.Join)
                    .SetFetchMode("Lines.Article", FetchMode.Join)
                    .Add(Restrictions.Eq("Id", orderId)).UniqueResult<Order>();


The 3th level is Article.
--
Dario Quintana
http://darioquintana.com.ar

Fabio Maulo

unread,
Aug 10, 2009, 6:16:54 PM8/10/09
to nhu...@googlegroups.com
but... you should understand the difference between <set> and <bag> ;)

P.S. <bag> es una bolsa de gatos.

2009/8/10 Dario Quintana <cont...@darioquintana.com.ar>



--
Fabio Maulo

Martin Nyborg

unread,
Aug 10, 2009, 6:21:48 PM8/10/09
to nhusers
Thanks for reply.

But I have 3 collection

var baselines = session.CreateQuery(" from Baseline b left join fetch
b.BaselineMilestones bm left join fetch bm.BaselineMilestonePrevious")
.SetResultTransformer
(Transformers.DistinctRootEntity)
.List<Baseline>();

I need all the baseline

each baseline has a collection of BaselineMilestones
and each BaselineMilestones has a collection of
BaselineMilestonePrevious

In our example:
all orders with all orderlines and Article need to be a collection on
orderlines, not a single class

hope that helps to explain my problem.



On Aug 10, 11:56 pm, Dario Quintana <conta...@darioquintana.com.ar>
wrote:
> If your question is how to do it, here you have an example:
>
> *select o from Order o join fetch o.Lines li join fetch li.Article where
> o.Id = :Id*
>
> OR
> *
> s.CreateCriteria(typeof (Order))
>                     .SetFetchMode("Lines", FetchMode.Join)
>                     .SetFetchMode("Lines.Article", FetchMode.Join)
>                     .Add(Restrictions.Eq("Id",
> orderId)).UniqueResult<Order>();*
>
> The 3th level is Article.
>
>
>
> On Mon, Aug 10, 2009 at 5:21 PM, mhnyborg <mhnyb...@gmail.com> wrote:
>
> > I just want to here if NH 2.1 has some new features that makes
> > selection from 3 levels possible.
>
> > for example I want to load all customers with all orders and order
> > lines. I can get the Customers and orders using
> > Transformers.DistinctRootEntity but that's only fixes the customer
> > collection I get to many orders.
>
> > The problem is better explained here:
>
> >http://stackoverflow.com/questions/332703/nhibernate-eager-fetching-o...
>
> --
> Dario Quintanahttp://darioquintana.com.ar
Reply all
Reply to author
Forward
0 new messages