Oren Eini (Ayende), due to some of the comments on his various blog posts regarding EF vs NH that compare some of the different features, has finally put up a post demonstrating how to load a larger object graph in a single trip to the database. So far, all of the examples have been how to load a single collection of related entities (a one-to-many relationship), or a single related many-to-one entity, but never both at the same time.
In his latest post, he demonstrates how to load a root entity, whilst eagerly fetching TWO one-to-many collections. While NH still issues two separate queries, they are at least sent in a single trip, and honestly, the simpler SQL is both easier to understand, should you be debugging your application and running SQL traces, and the total execution time of the SQL is probably less, since there are no complicated subselects, unions, and other things going on to do the work in a single query.
However, I’m not done fighting the good fight. Ayende’s latest example still does not show how to fetch a large, complex object graph, with multiple branches coming off the root entity, and with multiple levels of collections down each branch. I’ve challenged anyone and everyone to accomplish this in NHibernate since the first “versus” posts were published. After a few timid responses suggesting I use FetchMode.Subselect (which doesn’t work) or Futures (which I haven’t seen accomplish what I’m asking for), everyone started to silently ignore the issue. When InfoQ brought up the same “versus” debate, I posted in the comments there as well, calling for anyone that could load large, complex object graphs in a single trip (hopefully in a single query), with NHibernate.
Don’t get me wrong, I’m not saying “put your money where your mouth is” to the NHibernate guys — on the contrary, I still prefer NHibernate to EF, and while I’m going to give EF4 a fair chance, I want to see NHibernate compete in this specific regard. I want to know how to do it, because I want to use it in my own code!
Eager fetch multiple child collections in 1 round trip with NHibernate
By Sam | Published: January 18, 2010