Monday, March 9, 2009

Handling Cartesian Products in NHibernate

I have been using NHibernate for a little while now, but I am mostly self taught in this. I have recently done a course on ADO.NET to try and understand some of the basics of data access that I may have missed, and also to get a little of an understanding of the core technologies behind NHibernate.

I was reading a post on Eager Loading by Derik Whittaker on the Devlicio.us site where Derik was talking about returning a list of objects using the CreateCriteria query method. His issue was that when he used Eager Loading, he got more rows back then expected. This was due to the Cartesian Product issue where the join on the tables caused extra rows to be returned for a single object and NHibernate hydrating an object for each row. His solution was to include the command:

.SetResultTransformer( new DistinctRootEntityResultTransformer() )

which can be shortened to:

.SetResultTransformer(CriteriaUtil.DistinctRootEntity)

While reading through the comments on this post I found that someone had posted:

Why didn't you use the CreateMultiCriteria method?

This got me thinking. I have heard of MultiCriteria before but never realised that you could use it to solve the Cartesian Product problem before.

So to Google I went and I found the NHibernate Documentation and to my surprise I found the example under CreateMultiQuery. I had never found this before because I had always limited my reading to the CreateMultiCriteria (which is a more fluent version of MultiQuery).

So now the challenge I have set myself is to sit down and try to get some examples of this working.

Till next time.

Erik

No comments:

Post a Comment