Unable to use ICriteria with projection property that references a composite key relationship

Description

ADO.NET Provide: SqlClient

Entity A:
Key1
Key2

Entity B:
Key1
Key2
Key3

A has many B's:

Key1:Key1
Key2:Key2

Entity B property in hbm.xml:
<many-to-one name="A" >
<column name="Key1" />
<column name="Key2" />
</many-to-one>

Execute query:

session.CreateCriteria(typeof(B))
.SetProjection(Projections.Property("A"))
.List<A>();

ERROR: "property does not map to a single column"

EXCEPTION:
Original Message: property does not map to a single column: x3.Clientes Stack Trace: en NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumn(ICriteria criteria, String propertyName) en NHibernate.Criterion.PropertyProjection.ToSqlString(ICriteria criteria, Int32 loc, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) en NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetSelect(IDictionary`2 enabledFilters) en NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) en NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) en NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) en NHibernate.Impl.CriteriaImpl.List(IList results) en NHibernate.Impl.CriteriaImpl.List[T]()

This does work if the relationship is a single key.

Environment

None

Attachments

1

is related to

Activity

Show:

Stuart Carnie 
September 25, 2010 at 10:04 PM

Thanks for the fix, Julian! Much appreciated.

Julian Maughan 
September 25, 2010 at 8:09 PM

Please ignore my previous comments. I found some related tests and a solution in the Hibernate code base, and have ported them to NH (trunk r5220).

The tests are:
NHibernate.Test/Criteria/ProjectedComponent
NHibernate.Test/Criteria/ProjectedListIncludesComponent
NHibernate.Test/Criteria/ProjectedEmbeddedCompositeId
NHibernate.Test/Criteria/ProjectedCompositeId
NHibernate.Test/Criteria/ProjectedListIncludesEmbeddedCompositeId

I also committed your test, Stuart. Many thanks.

Julian Maughan 
August 19, 2010 at 6:44 AM

I think your query is back-to-front. In your test, you want to return a list of Employee, but you start with 'session.CreateCriteria<EmployeeAddress>'. Instead, how about:

var employees = s.CreateCriteria<Employee>()
.CreateCriteria("Addresses")
.Add(Restrictions.Eq("Type", "Postal"))
.List<Employee>();

Remember that HQL and the Criteria API are two different approaches to querying, so you should not expect them to work the same way.

Stuart Carnie 
May 18, 2010 at 2:32 PM

This patch created a unit test for in NHibernate.Test.csproj to demonstrate the issue.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created May 17, 2010 at 4:44 PM
Updated July 27, 2014 at 7:56 PM
Resolved September 25, 2010 at 8:09 PM
Who's Looking?