I have 3 classes (A,B,C) each of which is mapped to a table using nhibernate mapping files. I want to have a situation where:
A has a 1-M association to B
B has a 1-M association to C
and the HBM files have the ID field and one property that represents the foreign key (B.hbm.xml has one called FOREIGH_KEY_A and C.hbl.xml has one called FOREIGH_KEY_B)
I want to get all the rows in C that are related to A through these associations.
I know I can define "<one-to-many... " associations in the mapping files and then do: (pseudo code)
Code:
Iset intermediateResults = A.Bset();
for each B in intermediateResults
finalResults.append(B.Cset());
to get the results. However, this means fetching
every B object associated with A, where I really just want to retrieve those objects that are associated to Cs. (let's sat there is 1 A, a million Bs and only 1 of those has an associated list of Cs... I want to retrieve just 1 B not a million)
Given that all these classes are correctly mapped to their own tables, I can take out the "<one-to-many..." statements and just add my own mapped properties to B and C call FOREIGH_KEY_A and FOREIGH_KEY_B respectively. Then, an alternative way of doing this would be to use something like this HQL statement.
Code:
FROM MyNamespace.A a, MyNamespace.B b, MyNamespace.C c
WHERE b.FOREIGH_KEY_A = a.ID
AND c.FOREIGH_KEY_B = b.ID
this seems to be the right solution for me. the trouble is, I get the following exception from hibernate:
undefined alias or unknown mapping: MyNamespace [FROM MyNamespace.A a, MyNamespace.B b, MyNamespace.C c
WHERE b.FOREIGH_KEY_A = a.ID
AND c.FOREIGH_KEY_B = b.ID]
Any ideas????? do i need to have the "<one-to-many" statements in the mapping even though I am using straight HQL? or is the problem something more fundamental?