Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate集合双向映射_Hibernate_Collections_Mapping_Reverse - Fatal编程技术网

Hibernate集合双向映射

Hibernate集合双向映射,hibernate,collections,mapping,reverse,Hibernate,Collections,Mapping,Reverse,我有三个表user、product、user\u product,关系是user1:productn,用户pojo包含一个产品列表,而产品pojo包含一个用户 Class User{ List<Product>; } Class Product{ User; } User.hbm.xml <bag name="products" table="user_product" cascade="all" > <k

我有三个表user、product、user\u product,关系是user1:productn,用户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映射的产品列表。问题是,现在我必须从一些遗留应用程序迁移大量数据,因此无论何时添加具有用户关系的产品,我都必须获取用户及其产品列表,添加新产品并更新用户,这导致大量insert语句。因此,我创建了带有用户详细信息的产品pojo,并使用join标记完成了用户映射。我认为这样我可以只添加一个具有用户关系的产品,但当我保存一个产品时,它会使用用户id从用户产品中删除条目,然后插入新值,从而删除用户以前的所有产品。我做错了什么?

如果是一对多关系,为什么需要用户\产品表。您只能使用use和product表来实现这种关系。@javafan我知道这是由其他人完成的,我无法改变这一点。