Enterprise Java Development@TOPIC@

Chapter 62. Many-to-Many Relationships

62.1. Many-to-Many: Uni-directional
62.2. Many-to-Many: Bi-directional
62.3. Summary

Figure 62.1. Many-to-Many Uni-directional

Many-to-Many Uni-directional

Figure 62.2. Many-to-Many Bi-directional

Many-to-Many Bi-directional

Uni-directional

Only one side ("owner") knows of the relationship

  • Uses the @ManyToMany annotation

  • Defines mapping to database

    • Must use @JoinTable

Bi-directional

Both classes know of the relationship

  • One side required to be owning side and maps relation to the database

    • Uses the @ManyToMany annotation

    • Must use @JoinTable

    • Changes here change the database

  • One side required to be inverse and names the other entity's property

    • @ManyToMany(mappedBy="owning-property")

    • Changes here do *not* change database





  1. Rows added to join table rather than updating entity class tables with foreign key




  • Owning and inverse sides now both need to be set