Nick McKenna's Blog

Agile, Software, Technology
posts - 45, comments - 20, trackbacks - 45

Fun With NHibernate

I have had a maddening time this morning with NHibernate. I have a set of tables like this:

aspnet_users
Profile
aspnet_membership
Club

The scenario is that a user has a membership record and a profile. A profile specifies the club that the user belongs to. The club has an instructor (essentially another user).

I am presenting this information on screen in an ASP.Net application in a grid with these columns:

  Username, Surname, First Name, Initials, Club Name, Instructor Name, Email Address, Is Active

The user can sort ascending or descending by each of these fields.

A user does not have to have a profile with a valid club, so to get all the users listed, outer joins are required. I am using NHibernate and an HQL query to retrieve the data. All goes well until the user sorts by Club name. At this point, the records with no Club vanish from the grid...

A bit of digging (and SQL profiling) reveals that when an HQL query sorts by a column it has a bizarre WHERE clause that effectively eliminates any records that have null values. It took a while to work these out as the SQL generated by NHibernate is somewhat arcane.

I tried a couple of solutions relating to inserting a dummy record for "No Club", but this had its own share of issues. Eventually I hit upon the idea of creating a SQL Server View for the basic query with the outer joins. NHibernate seems quite happy to accept a View as a Table. I then let NHibernate do the relevant searching, sorting and row retrieval.

Hey Presto! The whole thing works within 10 minutes! All rows are displayed regardless of sort order and whether or not they are missing related records from a left outer join.

NHibernate is hard work. I hope that the full SP1 is out for .Net 3.5 soon so I can try Entities instead. I am resisting downloading the current Beta, but it starts to look more tempting the more I struggle with NHibernate...

Print | posted on Sunday, June 01, 2008 2:24 PM | Filed Under [ .NET ]

Feedback

Gravatar

# re: Fun With NHibernate

Ever consider using Linq to Sql as an ORM layer? It seems to be fairly stable and if you combine it with another layer of abstraction such as the Rob Conery's "repository" pattern it can provide a nice abstractions on top of POCO. For many projects Linq to SQL is all you need. http://www.asp.net/learn/3.5-extensions-videos/video-351.aspx
6/1/2008 4:54 PM | futureturnip
Gravatar

# re: Fun With NHibernate

Interesting, I couldn't recreate your bug at all. What was your HQL, and what NH version? I tried this without issues:

select u from u in class User left outer join fetch u.Club as c order by c.Name

The fetch join is to avoid the N+1 selects problem.

Output from a simple console app was this (3 users, 2 clubs, one user with no club assigned)

* homersimpson,

* tobinharris, Club A

* joebloggs, Club B


@futureturnip - Regarding Linq to SQL, I've enjoyed messing about with it, it's a great product for simple ORM IMHO. I'd still rather stick with NHibernate though in most cases - there's very few things it can't do, and it's incredibly extendible. Plus you can implement simple projects with very little mapping & code.

Looks like you might be a fellow Yorkshire .NET developer!? I'm based over in Leeds, do you ever make it to any developer meetups here?

12/1/2008 4:09 PM | Tobin Harris
Gravatar

# Mortgage calculator.

Mortgage loans.
1/12/2010 3:42 AM | Mortgage rates.
Gravatar

# ..

..
1/26/2010 4:20 PM | ..
Gravatar

# Health and fitness diet phentermine pill.

Health and fitness diet phentermine pill.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 7 and type the answer here:

Powered by: