The real issue
The answer here, based on the latest question updated, is clear and easy to fix! The mapping contains a wrong one-to-many
setting. See the first level list:
<bag name="Accounts" cascade="all" inverse="true">
<key column="FOOBAR_MASTER_ID" />
<!-- here we can see the CORRECT reference -->
<one-to-many class="FoobarAccount" />
</bag>
On the other hand, the second level is having the same target, which is wrong:
<bag name="TrackingRecords" cascade="all" inverse="true">
<key column="ACCOUNT_ID" />
<!-- WRONG. In deed, the Account does NOT contain 'PicNumber' -->
<one-to-many class="FoobarAccount" />
</bag>
The answer:
Change the <one-to-many class="FoobarAccount" />
into <one-to-many class="TrackingRecord" />
Correct mapping should be like this:
<bag name="TrackingRecords" cascade="all" inverse="true">
<key column="ACCOUNT_ID" />
<!-- now we won't recieve the Account does not contain 'PicNumber' -->
<one-to-many class="TrackingRecord" />
</bag>
From that moment, all the stuff will work properly, the query in the begining of the question is correct. No need for subqueries etc
...
Original tips - related to previously available information
What you are trying achieve would work if your object/entities would be chained like this
Master
has-many (or references) AccountsAccount
has-many (or references) TrackingRecords
But based on the issue you have, it seems that your mapping is
Master
has-many (or references) AccountsMaster
has-many (or references) TrackingRecords
In that case, you can only achieve SQL Like this
select m.* from t_master m
inner join t_account a on m.master_id = a.master_id
//inner join t_tracking t on a.account_id = t.account_id
inner join t_tracking t on m.account_id = t.account_id // the m.account_id
where t.tracking_no = '123456'
And the query should be like this:
// do some filter over A
var rootQuery = session.CreateCriteria<Master>("M")
.CreateCriteria("Accounts", "A", NHibernate.SqlCommand.JoinType.InnerJoin);
// working with the T here
rootQuery.CreateCriteria("TrackingRecords", "T", NHibernate.SqlCommand.JoinType.InnerJoin)
.Add(Restrictions.Eq("T.TrackingNo", "123456"));
Update, reflecting the question extension:
This mapping does not fit together:
The key column of the TrackingRecords is ACCOUNT_ID
<class name="Account" table="T_ACCOUNT">
...
<bag name="TrackingRecords" cascade="all" inverse="true">
<key column="ACCOUNT_ID" />
...
While the reference from TrackingRecord is done via ACCOUNT_NUMBER
<class name="TrackingRecord" table="T_TRACKING">
...
<many-to-one name="Account" class="Account" column="ACCOUNT_NUMBER" />
...