ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 6.3.2\. 双向关联(Bidirectional associations) _双向关联_允许通过关联的任一端访问另外一端。在Hibernate中, 支持两种类型的双向关联: 一对多(one-to-many) Set或者bag值在一端, 单独值(非集合)在另外一端 多对多(many-to-many) 两端都是set或bag值 要建立一个双向的多对多关联,只需要映射两个many-to-many关联到同一个数据库表中,并再定义其中的一端为_inverse_(使用哪一端要根据你的选择,但它不能是一个索引集合)。 这里有一个many-to-many的双向关联的例子;每一个category都可以有很多items,每一个items可以属于很多categories: ``` <class name="Category"> <id name="id" column="CATEGORY_ID"/> ... <bag name="items" table="CATEGORY_ITEM"> <key column="CATEGORY_ID"/> <many-to-many class="Item" column="ITEM_ID"/> </bag> </class> <class name="Item"> <id name="id" column="CATEGORY_ID"/> ... <!-- inverse end --> <bag name="categories" table="CATEGORY_ITEM" inverse="true"> <key column="ITEM_ID"/> <many-to-many class="Category" column="CATEGORY_ID"/> </bag> </class> ``` 如果只对关联的反向端进行了改变,这个改变_不会_被持久化。 这表示Hibernate为每个双向关联在内存中存在两次表现,一个从A连接到B,另一个从B连接到A。如果你回想一下Java对象模型,我们是如何在Java中创建多对多关系的,这可以让你更容易理解: ``` category.getItems().add(item); // The category now "knows" about the relationship item.getCategories().add(category); // The item now "knows" about the relationship session.persist(item); // The relationship won''t be saved! session.persist(category); // The relationship will be saved ``` 非反向端用于把内存中的表示保存到数据库中。 要建立一个一对多的双向关联,你可以通过把一个一对多关联,作为一个多对一关联映射到到同一张表的字段上,并且在"多"的那一端定义`inverse="true"`。 ``` <class name="Parent"> <id name="id" column="parent_id"/> .... <set name="children" inverse="true"> <key column="parent_id"/> <one-to-many class="Child"/> </set> </class> <class name="Child"> <id name="id" column="child_id"/> .... <many-to-one name="parent" class="Parent" column="parent_id" not-null="true"/> </class> ``` 在“一”这一端定义`inverse="true"`不会影响级联操作,二者是正交的概念!