English 中文(简体)
表格中外列的许多对手法
原标题:Many-to-many with extra columns on join table

如何确定这种关系? 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);

得到帮助!

最佳回答

你需要把你的许多与人的关系分成两条,就像在你的数据库中那样。

So, one User has many UserProduct items, and one Product has many UserProduct items. A UserProduct has one User and one Product.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签