I’m facing the following situation.
My objectmodel is as follows:
EntityA --*> EntityB -> EntityC
EntityA references a list of EntityB. EntityB references a single EntityC
My mapping is:
<class name="EntityA" table="TAB_A" >
<id name="Id" column="A_ID" >
<generator class="native" />
</id>
<version name="Version" column="VERSION" />
<bag name="listOfB" cascade="save-update" table="A_TO_C" lazy="false" >
<key column="FK_A_ID" />
<composite-element class="EntityB" >
<many-to-one name="refToC" class="EntityC" column="FK_C_ID" cascade="save-update" />
</composite-element>
</bag>
</class>
I left out all the extra properties and stuff.
I query the DB:
var query = session.Query<EntityA>()
.Where(a=> a.listOfB.Any(b => b.refToC == anInstanceOfC));
// count all matches
var count = query.ToFutureValue(t => t.Count());
Okay, that looks good for me so far ;). When I execute the query I get an error, that an SQL-Statement fails. The generated SQL contains the following sub statement:
select listOfB1_.A_ID
from A_TO_C listOfB1_
where a0_.A_ID=listOfB1_.FK_A_ID
and listOfB1_.FK_C_ID=?
The statement expects a column A_ID on table A_TO_C (which actually is the column name of the id-column of TAB_A). What it should do, is selecting the column listOfBl_.FK_A_ID wich is the foreign-key-column referencing the A_ID column of TAB_A.
Now the question: Am I wrong or is it the SQL-generator ?
Actually it is a bug.
See another thread on stackoverflow,
my question and discussion on the nhibernate googlegroup
and the actual bug report in the nhibernate issue tracker