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