Questo contenuto non è disponibile nella lingua selezionata.
6.3.2. Bidirectional associations
A bidirectional association allows navigation from both "ends" of the association. Two kinds of bidirectional association are supported:
- one-to-many
- set or bag valued at one end and single-valued at the other
- many-to-many
- 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:
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
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
inverse="true"
.
Mapping one end of an association with
inverse="true"
does not affect the operation of cascades as these are orthogonal concepts.