Wednesday, 2 December 2009

NHibernate Querying across Composite Keys

Error Message: ‘The multi-part identifier “manager1_.MAN_ID” could not be bound’

Firstly, let me say I do not like composite keys. Unique constraints work much better. That said, NHibernate does have support for Composite Keys, for these legacy databases, and it is fairly intuitive with the intellisense. However, I have come across a bizarre issue when querying across these relationships.

Database Diagram

Project-Managers Schema

This is the database diagram. Nothing too special about it. Projects have Managers and managers can manager numerous projects.

It is worth saying that the same issue occurs with a two way composite key as well as this three way version. The project roles could probably be ignored since the ‘role’ is stored in the Managers table.

Classes / Mappings

One thing to note, that you have to override the Equals and GetHashCode methods to use with NHibernate.

Project

ProjectClass
again some lines have been removed for brevity.

ProjectMapping

ProjectMember

ProjectMemberClass

ProjectMemberMapping

This is quite interesting example of the composite-id mapping. There are three that make up the key and all are many-to-one elements that map to classes in their own right.  

Manager

ManagerClass

ManagerMapping

Some of the above namespaces have been blurred for client confidentiality.

The Problem

I am currently working on a large search mechanism across various fields of the application. I am basing the search on the blog from Ayende Rahien so I create a DetachedCriteria object for the Project and then everything is based from that.

The method in question is as follows:

ManagersQuery

A little explanation. criteria (IAdvancedSearchCriteria) is my class which holds the search data. In this case, it is the LineManagerIds pulled from a multiple select box.

So this code runs and is then executed against the current NHSession and using NHProfiler I can review the SQL being generated:

NHSQLError

If you remember, this should link projects –> Project Members –> Managers in a Many to Many style situation (although it is mapped as Many to one for the audit information)

Look at the inner SQL and you can see there is no mention of the Managers table. Why? I created an Association to the Manager object. It merely results in a list of errors like ‘The multi-part identifier “manager1_.MAN_ID” could not be bound’.

I assume it is something to do with composite-ids (Did I mention I don’t really like these…)

the association to the Manager is mapped up in the <composite-id> section of the mapping file. Since I have made many searches before, I deduce there is a high probability that this may have something to do with it.

So I assume that NHibernate is not using the Class association in the composite-id section. I could either re-design the database to remove the Composite Id (Tempting… did i mention i hate them?) but my remit is to leave the database alone…

So, to fix the problem, I help NHibernate out. If I add another <property> to the ProjectMembers mapping file for the Manager, I can make the query work.

FixedMapping

Now, an important thing to note. I assume, NHiberate will do the inserting and updating based on the <composite-id> mapping, so I have set insert and update = “false” otherwise I think we’d have some confusing errors / SQL statements. I will update this post if this proves incorrect! but it has solved the searching mechanism as shown:

FixedSQL

As you can see, we are now joining in on the managers table. There was no change to the code, just the mapping file.

Tuesday, 13 October 2009

NHibernate – When ‘Load’ does ‘Load and Update’

I came across a truly bizarre update occurring within my Gemini Mailbox Processer Application. It was reported to me by a friend who did some monitoring on the application. We were both completely baffled why NHibernate would be doing any form of update on a simple List<IEmail> call. Let’s look at the details.

Firstly, let’s see the interface which defines the class. I have removed some fields for brevity.

 3_Interface

Then here is our code which will load the Emails. Here we create our criteria object, and add some filtering, some ordering, and then retrieve a list of IEmail objects. Nothing seems strange here.

1_Code

This is the mapping (abbreviated version) to the email class. You might spot the issue in here (but I’ll spell it out later)

2_Mapping

Ok, so running this, let’s use NHibernate Profiler to see what is going on.

4_ProfilerUpdate

As you can see, there is a bizarre update statement. At least NHibernate was batching it up into one statement, but WHY! Let’s look at the specific details for this query. Again some items have been removed to keep the snippet small.

5_ProfilerDetails

I decided to comment out lines in the mapping and run a test app to see what results were returned. Each time I ran it until I saw this update appear. Trial an error narrowed it down to the Priority column.

We can see the priority is looking a little strange. In the interface it is an enum, and in the mapping it is specified as an Int32. So it seems strange that NHibernate it sending it as Text? Let’s remove the Type=”Int32” from the mapping and see what happens.

6_ChangeMapping

Rerunning it shows it is all ok now. So while I do not know why, I do now know how to prevent it. I assume it is to do with the type conflict between the enum and specifying it as an Int32.

7_ProfilerFixed

Windows Live?

Apparently you can make posts with Windows Live. It seems fairly straightforward to do.

Just enter your URL, your user name and password and it logs in and downloads stuff. We will see if this post makes it to the blog, or I’ve just compromised the security of the blog :-)

Thursday, 30 July 2009

Could not load file or Assembly CustomMarshalers.dll

I had an interesting error after installing VS2008 and trying to create ANY new project. I tried, web, console, services, and none would be created. Each time I had the following error:

Could not load file or assembly CustomMarshalers, Version=2.0.0.0 etc

For some reason, this file was not installed into the GAC

c:\>gacutil -i C:\Windows\Microsoft.NET\Framework\v2.0.50727\CustomMarshalers.dll

command into the VS2008 command prompt solved the issue.

Useful Links for New Projects

NHibernate Download
NHibernate Homepage

NHibernate Profiler

Resharper

NVelocity and Engine Wrapper


Template Web Project Structure using NHibernate and NVelocity