many-to-many bidirectional association

/, JPA/many-to-many bidirectional association

many-to-many bidirectional mapping :

In a relational database in a many-to-many relationship, a row in table X can have  more than one matching row in table Y, a row in table Y can have more than one matching row in table X.

In Object oriented programming, one instance of entity refers to multiple instances of another entity and an instance of that another entity refers to multiple instances of the other entity in the  relation called many-to-many relation.

In this tutorial used the relationship between Engineering Branch and Subjects that are having each Engineering branch in a semester. In an Engineering college each Branch have many Subjects and each Subject may appear in many Engineering branch semester.

many-to-many  Tables structure in Database :

what is join table?

A join table is a table in database that represents the multiple references of one or more table . In other words a join table is used to refer multiple records of one or more tables by using foreign key constraints. Below image illustrates more about many-to-many association.

what is bidirectional?

  • Bidirectional relationship provides navigational access in both directions, so that you can access the other side entity without explicit queries.

Subject.java mapping :

Branch.java mapping :

@JoinTable

    • @JoinTable Used in the mapping of associations. It is specified on the owning side of an association.

    • @JoinTable can be used with embeddable types as well.

      When a join table is used in mapping a relationship with an embeddable class on the owning side of the relationship, the containing entity rather than the embeddable class is considered the owner of the relationship.

To Understand more about @JoinTable mapping in entity associations see :
Key points to understand associations

many-to-many  bidirectional mapping testing :

Console output :


Download Application – JPA-ManyToMany-BiDirection.zip (16 KB)

Leave A Comment