A bidirectional association
allows navigation from both "ends" of the association. Two kinds of bidirectional association are supported:
set or bag valued at one end and single-valued at the other
set or bag valued at both ends
You can specify a bidirectional many-to-many association by mapping two many-to-many associations to the same database table and declaring one end as inverse. You cannot select an indexed collection.
Here is an example of a bidirectional many-to-many association that illustrates how each category can have many items and each item can be in many categories:
<id name="id" column="CATEGORY_ID"/>
<bag name="items" table="CATEGORY_ITEM">
<many-to-many class="Item" column="ITEM_ID"/>
<id name="id" column="ITEM_ID"/>
<!-- inverse end -->
<bag name="categories" table="CATEGORY_ITEM" inverse="true">
<many-to-many class="Category" column="CATEGORY_ID"/>
Changes made only to the inverse end of the association are not persisted. This means that Hibernate has two representations in memory for every bidirectional association: one link from A to B and another link from B to A. This is easier to understand if you think about the Java object model and how a many-to-many relationship in Javais created:
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 will not be saved!
session.persist(category); // The relationship will be saved
The non-inverse side is used to save the in-memory representation to the database.
You can define a bidirectional one-to-many association by mapping a one-to-many association to the same table column(s) as a many-to-one association and declaring the many-valued end
<id name="id" column="parent_id"/>
<set name="children" inverse="true">
<id name="id" column="child_id"/>
Mapping one end of an association with
inverse="true" does not affect the operation of cascades as these are orthogonal concepts.