NHibernate 2.0.1GA:
Java version of many-to-many mapping has attribute (property-ref), but Nhibernate has foreign-key. Foreign-key doesn't seem to work in following example.
TRADE_ACCOUNT(Primary Key: ACCOUNT_ID)
MASTER_ACCOUNT(Primary Key: ORG_MSTRACCT_ID, Unique Key: MSTR_ACCT_ID)
MSTR_SUB_ACCT (Primary Key: MSTR_ACCT_ID, SUB_ACCT_ID)
Joins needs to be done as below:
TRADE_ACCOUNT.ACCOUNT_ID = MSTR_SUB_ACCT.SUB_ACCT_ID
MASTER_ACCOUNT.MSTR_ACCT_ID = MSTR_SUB_ACCT.MSTR_ACCT_ID
Relavent mapping is as below:
Code:
<class name="TradeAccount" table="TRADE_ACCT" mutable="false">
<id name="id" column="ACCOUNT_ID">
<generator class="assigned" />
</id>
<bag name="masterAccounts" table="MSTR_SUB_ACCT" >
<key column="SUB_ACCT_ID"/>
<many-to-many class="MasterAccount" column="MSTR_ACCT_ID" foreign-key="masterAccountId"/>
</bag>
</class>
<class name="MasterAccount" table="CB_ORG_MSTRACCT">
<id name="id" column="ORG_MSTRACCT_ID">
<generator class="identity"/>
</id>
<property name="masterAccountId" column="MSTR_ACCT_ID"/>
<bag name="subAccounts" table="MSTR_SUB_ACCT">
<key column="MSTR_ACCT_ID" property-ref="masterAccountId"/>
<many-to-many class="TradeAccount" column="SUB_ACCT_ID"/>
</bag>
</class>
Following code is generating wrong Joins:
ICriteria criteria = session.CreateCriteria(typeof (TradeAccount), "ta");
criteria.CreateCriteria("masterAccounts", "ma").Add(Expression.Eq("ma.id", maId));
Joins needs to be done as below:
TRADE_ACCOUNT.ACCOUNT_ID = MSTR_SUB_ACCT.SUB_ACCT_ID and
MASTER_ACCOUNT.
MSTR_ACCT_ID = MSTR_SUB_ACCT.MSTR_ACCT_ID
Instead it is doing:
Joins needs to be done as below:
TRADE_ACCOUNT.ACCOUNT_ID = MSTR_SUB_ACCT.SUB_ACCT_ID and
MASTER_ACCOUNT.
ORG_MSTRACCT_ID = MSTR_SUB_ACCT.MSTR_ACCT_ID
This seems to be bug in Nhibernate. Please let me know if there is a workaround. Thanks.