NHibernate Eager Fetching Over Multiple Levels with StatelessSession?

I suspect this is because the sql query generated produces duplicate applications as the roles were joined.

You're getting 4XOrder and 4XLines because the join with lines doubles the results . You can set a Transformer on the ICriteria like.

You're getting 4XOrder and 4XLines because the join with lines doubles the results . You can set a Transformer on the ICriteria like : . SetResultTransformer(new DistinctRootEntityResultTransformer()).

2 I discovered that this does actually solve the problem, if the mappings are changed from bag to set, and I implement the necessary IComparable on the base class. – Ben Laan Jan 7 '09 at 21:52 1 Interesting, I have used the DistinctRoot .... but always with a Bag, and never implementing IComparable. But never did go for 3 level loading :) – sirrocco Jan 8 '09 at 5:44 Also, I think it would be best not to Load the customer and then go Customer.Orders.In general you don't ask a customer to tell you his orders.

So it would be best to have a Repo and : GetOrdersForCustomerId(int id). – sirrocco Jan 8 '09 at 5:45 3 Using . SetResultTransformer(new DistinctRootEntityResultTransformer()) for a two level eager load is fine, more than that starts producing cartesian joins – Rippo Nov 1 '10 at 15:51.

I just read Ayende's Blogpost where he used the following Example: session. CreateCriteria(typeof(Post)) . SetFetchMode("Comments", FetchMode.

Eager) .List(); In a Criteria Query to avoid Lazy Loading on one particular Query Maybe that can help you.

You just saved me a handful of hours regression testing because of this comment. Much obliged. – joshua.

Ewer Sep 2 '09 at 18:45 That won't work "over multiple levels" as the questions states. Using FetchMode. Eager for multiple levels will result in a cartesian product.

The correct SQL is generated, but Nbernate won't sort it out for you. – Samuel Meacham Jan 4 '10 at 20:16.

Tigraine: your query only returns Post with Comments. This brings All posts with all Comments (2 levels). What Ben asking is Customer to Order To LineItem (3 level).

@Ben: to my knowledge nbernate doesn't support eager loading upto 3 level yet. Bernate does support it thou.

I was having the same problem. See this thread. I didn't get a solution but a hint from Fabio.

Use Set instead of bag. And it worked. So my suggestion is try to use set.

You don't have to use Iesi collection use IDictonary and NH is happy public override IEnumerable GetAll() { var baselines = Session. CreateQuery(@" from Baseline be left join fetch b. BaselineMilestones bm left join fetch bm.

BaselineMilestonePrevious ") . SetResultTransformer(Transformers. DistinctRootEntity) .List(); return baselines; }.

Switching from to worked for me as well, but a SQL trace shows that a Cartesian product is still produced on the server. This means Nbernate is sorting through and filtering the results on its end, to correctly populate the child and grand-child collections. This probably won't be ideal in many situations, as it may mean thousands or millions of records being dragged across the wire, and then a lot of processing to construct the correct object graph.

– Samuel Meacham Jan 4 '10 at 23:34.

If you need to keep your one-to-manys as bags, then you can issue 2 queries, each with only 1 level of hierarchy. Eg something like this: var temp = session. CreateCriteria( typeof( Order ) ) .

SetFetchMode( "Lines", NFetchMode. Eager ) . Add( Expression.

Eq( "Customer. ID", id ) ) .List(); var customer = session. CreateCriteria( typeof( Customer ) ) .

SetFetchMode( "Orders", NFetchMode. Eager ) . Add( Expression.

Eq( "ID", id ) ) .UniqueResult(); Lines get loaded into the NH cache in the first query, so they won't need lazy loading when later accessing eg customer. Orders0. Lines0.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions