Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Eager loading a One-to-Many collection w/ NHibernate

At work I have a section of code which I need to eager load all my data in order to pass that data back across the wire.  I thought I had it all setup correctly, but then I realized that I was not returning back unique results from my query.

Imagine I have the following Schema

image

My current need was to get all Destinations in the database (we know it is a small number and this is for the edit screen) and have it eager load the endpoint (which you actually get for free) and the Assigned_Message_Types

Originally setup my Criteria selection as below

var result = Session.CreateCriteria( typeof( Destination ) )                                
	.SetFetchMode( "Endpoint", FetchMode.Eager )
        .SetFetchMode( "AssignedMessageTypeses", FetchMode.Eager )        
        .List();

When I ran the query above, I expected to get 2 records back, one for each destination in the database.  However, I received 4 of them back.  The first 3 were the same, and the 4th one was different.  As you could imagine this is NOT what I expected.  I thought I would reach out to Kyle Baley (@kbaley) for some help on this, but turns out he was having the same issue recently.  Good thing for both of us the Twitterverse was able to lend a helpful hand.  James Kovacs (@jamesKovacs) along with someone else (sorry, forgot our handle) pointed both of us to the following 2 posts.

I decided to get the usage of DistinctRootEntityResultTransformer a try, and the code below is what worked for me.

var result = Session.CreateCriteria( typeof( Destination ) )                                
	.SetFetchMode( "Endpoint", FetchMode.Eager )
        .SetFetchMode( "AssignedMessageTypeses", FetchMode.Eager )        
	
	// this is the magic line    
        .SetResultTransformer( new DistinctRootEntityResultTransformer() )
        .List();

By adding the DistinctRootEntityResultTransformer transformer to my query I now get 2 rows of data, which is exactly what I needed/wanted.

Till next time,


Posted 03-05-2009 3:14 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Erik Nilsson wrote re: Eager loading a One-to-Many collection w/ NHibernate
on 03-05-2009 5:25 PM

Hey Derik. I had that problem a while ago and after a Google search I found a similar solution. Instead of instantiating the DistinctRootEntryResultTransformer you can use CriteriaUtil.DistinctRootEntity. Slightly shorter.

DotNetShoutout wrote Eager loading a One-to-Many collection w/ NHibernate - Derik Whittaker
on 03-05-2009 5:29 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetKicks.com wrote Eager loading a One-to-Many collection w/ NHibernate
on 03-05-2009 5:38 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

nHibernate_newbie wrote re: Eager loading a One-to-Many collection w/ NHibernate
on 03-07-2009 6:49 AM

what is wrong with "session.CreateMultiCriteria()" ?

just asking as you're pointing to the article that uses exactly that.

Nhibernate_newbie wrote re: Eager loading a One-to-Many collection w/ NHibernate
on 03-07-2009 7:38 AM

2nd though, shouldn't the use of .SetResultTransformer(new DistinctRootEntityResultTransformer())  be prohibited/not adviced?

If I understand it right, NHibernate will only remove the duplicates in your code (e.g. resultset List<T> etc), but the call to the DB will be everything but not optimal.

Dealing with thousands and thousands of results will probably show off more then dealing with 3 of them where you don't really care.

Then maybe I might be wrong and there is some magic, as the whole eager loading of NHibernate seems uterly complicated compared to some OR/M I'm used to (lightspeed) which do it in absolutely elegant way (but there is a need of attribute usage, which goes against POCO models).

Thanks for answer Derik

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)