For a simple many-to-many database relationship, you can use the
@ManyToMany JPA annotation and, therefore, hide the join table.
However, sometimes you need more than the two Foreign Key columns in the join table, and, for this purpose, you need to replace the
@ManyToMany association with two bidirectional
@OneToMany associations. Unlike unidirectional
@OneToMany, the bidirectional relationship is the best way to map a one-to-many database relationship that requires a collection of Child elements on the parent side
In this article, we are going to see how you can map a many-to-many database relationship using an intermediary entity for the join table. This way, we can map additional columns that would be otherwise impossible to persist using the
@ManyToMany JPA annotation.