如何确定这种关系? I'sreade具有许多至many realtionship,但现在有新特点,需要在表格中增加一栏。
使之简单,即有一个用户类别、产品类别和用户类别,今天,情况就是如此。
产品.hbm.xml
<bag name="Users" table="users_products" inverse="true" cascade="save-update" >
<cache usage="read-write" />
<key column="ProductId" />
<many-to-many class="User" column="UserId" outer-join="true" />
</bag>
<bag name="UserProduct" inverse="true" cascade="save-update">
<cache usage="read-write" />
<key column="ProductId" />
<one-to-many class="UserProduct" />
</bag>
用户hbm.xml
<bag name="Products" table="users_products" inverse="true" cascade="save-update" >
<cache usage="read-write" />
<key column="UserId" />
<many-to-many class="Product" column="ProductId" />
</bag>
<bag name="UserProduct" inverse="true" cascade="save-update">
<cache usage="read-write" />
<key column="UserId" />
<one-to-many class="UserProduct" />
</bag>
How should the cascade look like? Now for the new features i created a new class UserProduct:
private int _id;
private User _user;
private Product _product;
private string _ownComment;
And created User产品.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="" assembly="">
<class name="UserProduct" table="users_products">
<cache usage="read-write"/>
<id name="Id" column="UsersWhiskiesId" type="Int32" length="4" unsaved-value="0">
<generator class="native" />
</id>
<many-to-one name="User" class="User" column="UserId" not-null="true" />
<many-to-one name="Product" class="Product" column="ProductId" not-null="true" />
<property name="OwnComment" column="OwnComment" type="String" />
</class>
</hibernate-mapping>
If i earlier have used user.Products.Add(product); how will it look like now? How will i map the user and product? How do i update an excisting record? When i delete the user i want to delete the association to the product and not the product.
NEW QUESTION: I have a page where registered users can collect products. On this page i now want the user to be enabled to hide some of there products. I have a gridview populated and a templatefield with checkbox. How do i update the UserProduct where i have a column "HideProduct". I have this code in a button.
for (int i = 0; i < this.gvProducts.Rows.Count; i++)
{
GridViewRow row = this.gvProducts.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
int productId=
Convert.ToInt32(this.gvProducts.DataKeys[row.RowIndex].Value);
UserProduct up= new UserProduct();
up.User = this._user;
up.Product= DataManagement.CoreRepository.GetObjectById<Product>(productId);
up.HideProduct = isChecked;
DataManagement.CoreRepository.UpdateObject(up);
得到帮助!