Java Reference
In-Depth Information
What gets interesting is if you answer yes to both of these questions. If so, you'll want to
read more about the
@ManyToMany
relationship in the next section.
9.4.3. Many-to-many relationships
While not as common as one-to-many, many-to-many relationships occur quite frequently
in Enterprise applications. In this type of relationship, both sides might have multiple ref-
erences to related entities. In the ActionBazaar example, the relationship between
Cat-
Figure 9.8. A many-to-many relationship between
Item
and
Category
In the
Item-Category
relationship, an
Item
can be placed on multiple categories, and
at the same time a
Category
can contain multiple
Item
s. With JPA, the
@ManyToMany
annotation is used to configure this relationship. Typically, many-to-many relationships are
bidirectional.
Listing 9.20
shows an example of a bidirectional many-to-many relationship
between
Item
and
Category
.