Hibernate集合双向映射

时间:2014-09-30 18:12:36

标签: hibernate collections mapping reverse

我有三个表 user,product,user_product ,关系是 user(1):product(n),用户pojo包含产品和产品列表pojo包含一个用户。

 Class User{
        List<Product>;
 }

 Class Product{
     User;
 }

User.hbm.xml

   <bag name="products" table="user_product" cascade="all" >
        <key>
            <column name="user_id" not-null="true" />
        </key>
        <many-to-many entity-name="Product">
            <column name="product_id" not-null="true" unique="true"/>
        </many-to-many>
    </bag>

Product.hbm.xml

   <join table="user_product"  optional="true">
       <key column="product_id" />
       <many-to-one name="user" column="user_id" cascade="none"  />
    </join>

用户的映射文件包含使用 bag 映射的产品列表。问题是现在我必须从一些遗留应用程序迁移大量数据,所以无论何时添加具有用户关系的产品,我都必须获取用户和他的产品列表并添加新产品并更新用户,这导致很多插入语句。因此,我使用用户详细信息创建了产品pojo,并使用加入标记完成了用户映射。我这样认为我只能添加一个具有用户关系的产品,但是当我保存产品时,它会使用 user_id user_product 中删除条目,然后插入新值删除用户以前的所有产品。我在这里做错了什么?

0 个答案:

没有答案
相关问题