Derik Whittaker

Syndication

News


NHibernate and “The multi-part identifier … could not be bound” Exception

Today as I was creating a new NHibernate Criteria statement everything was working fine UNTIL I added the where clause.  At this point I started to get the following exception (which was seen via NHibernate Profier).

WARN:
System.Data.SqlClient.SqlException: The multi-part identifier "doc2_.NAME" could not be bound.

My criteria statement looked something like this

var query = Session.CreateCriteria( typeof(DocumentSection), "docSection" )
    .CreateAlias( "docSection.DocumentSegment", "docSegment", JoinType.None )
    .CreateAlias( "docSegment.DocumentDefinition", "doc", JoinType.InnerJoin )
    .... MORE STUFF HERE

    .Add( Restrictions.Eq( "doc.Name", documentName ) )

    .SetResultTransformer( new DistinctRootEntityResultTransformer() )
    .List();

When I first saw this error it did not make any sense as I KNEW that there was in fact a Name field on the object I was trying to hydrate.  But if you look closely at the first .CreateAlias you will notice the issue.  For my JoinType I had set it to JoinType.None and not JoinType.Inner so when NHibernate wanted to hydrate my object it could not because as far as it was concerned the object was not meant to be hydrated.

To resolve this issue I changed the JoinType to be Inner and all worked.  Below is the working code.

var query = Session.CreateCriteria( typeof(DocumentSection), "docSection" )
    .CreateAlias( "docSection.DocumentSegment", "docSegment", JoinType.InnerJoin )
    .CreateAlias( "docSegment.DocumentDefinition", "doc", JoinType.InnerJoin )
    .... MORE STUFF HERE

    .Add( Restrictions.Eq( "doc.Name", documentName ) )

    .SetResultTransformer( new DistinctRootEntityResultTransformer() )
    .List();

Till next time


Posted 01-05-2010 6:48 AM by Derik Whittaker
Filed under: ,

[Advertisement]

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)