NHibernate is designed to work by default with two different tables for this
case.
I am not sure if this is fully supported but you might try:
<bag name="categories" table="Stock_Category" cascade="all" lazy="false"
where="TYPE=1">
<key column="STOCK_ID" />
<many-to-many class="Category" column="CATEGORY_ID" />
<sql-insert>
INSERT INTO Categories(Type, Stock_Id, Category_Id) values(1,?,?)
</sql-insert>
<sql-delete>
DELETE FROM Categories where Stock_Id = ? and Category_Id = ? and
Type = 1
</sql-delete>
<sql-delete-all>
DELETE FROM Categories where Stock_Id = ? and Type = 1
</sql-delete-all>
</bag>
<bag name="categories2" table="Stock_Category" cascade="all"
lazy="false" where="TYPE=2">
<key column="STOCK_ID" />
<many-to-many class="Category" column="CATEGORY_ID" />
<sql-insert>
INSERT INTO Categories(Type, Stock_Id, Category_Id) values(2,?,?)
</sql-insert>
<sql-delete>
DELETE FROM Categories where Stock_Id = ? and Category_Id = ? and
Type = 2
</sql-delete>
<sql-delete-all>
DELETE FROM Categories where Stock_Id = ? and Type = 2
</sql-delete-all>
</bag>
Alternatively (if you are using sql server) you can create two different
updatable views.
Then you will ned to write an "Instead OF" trigger.
2011/5/14 sando <[email protected]>
> I am stuck at a problem where we have multiple many-to-many
> relationships to be mapped from a POCO to a single mapping table.
> Unfortulately I can not change the POCO, neither the DataModel.
>
> To differentiate multiple M-M we have a field in our mapping table of
> type int.
> This filed has to be set while we insert into the mapping table.
>
> This would help us to use "where" to fetch them into appropriate
> collection while retrieving.
>
> While writing the hbm for it I saw that while insert everything works
> fine, but the TYPE column remains 'NULL'
>
> Consequently, while retrieve everything comes to all the collections
> in the POCO; if we do not put the "where" clause.
> If we put the "where" then both collections remain empty.
>
> Assume the class model as follows:
>
> class Stock
> {
> public int? Id {get; set;}
> public string StockName {get; set;}
>
> public IList<Category> PublicCategories {get; set;}
> public IList<Category> PrivateCategories {get; set;}
> }
>
> class Category
> {
> public int? Id {get; set;}
> public string CategoryName {get; set;}
> }
>
> I have 2 tables:
> StockTable
> [id(int), Name(varchar)]
>
> CategoryTable
> [id(int), Name(varchar)]
>
> Stock_Category_Table
> [Stock_id(int), Category_id(int), TYPE(int)]
>
> This is my mapping, partially:
>
> <bag name="categories" table="Stock_Category" cascade="all"
> lazy="false" where="TYPE=1">
> <key column="STOCK_ID" />
> <many-to-many class="Category" column="CATEGORY_ID" />
> </bag>
> - <bag name="categories2" table="Stock_Category" cascade="all"
> lazy="false" where="TYPE=2">
> <key column="STOCK_ID" />
> <many-to-many class="Category" column="CATEGORY_ID" />
> </bag>
>
> I tried putting multiple columns in the mapping table and check for
> not null as proposed in:
> https://forum.hibernate.org/viewtopic.php?f=25&t=953401
>
> This works, but the solution is not elegant and does not support our
> project design.
> We need a way to set the mapping table column "TYPE" when we insert
> every record.
>
> Regards,
> sando
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.