Using @ManyToMany annotation in JPA

In this tutorial, we will research together on a final annotation in JPA, beside @ManyToOne, @OneToOne, @OneToMany annotation, to express the relationship between any two tables in the database. That is the annotation @ManyToMany! In case, any two tables have many-many relationships, such as a developer can do in many different projects and a project can have many developers, then you can use the @ManyToMany annotation to express that relationship. Let’s find out how to use this annotation in this tutorial.

First, I will create a Maven project as an example:

Using @ManyToMany annotation in JPA

I will use Hibernate as JPA implementation so I will add Hibernate dependency as follows:

MySQL Connector:

Assume, now I define the structure of the two tables: developer and project as follows:

As you can see, I’ve defined the developer table with two columns, id and name, where id is the primary key. In the project table, I also define the two columns id and name with the primary key is id.

Here, we can not define foreign key columns from one table to another because it will not show the relationship many-many between them. For example: if you define a project_id column in the developer table, you only define a project that the developer belongs to, can not define multiple projects. In contrast to the project table, it is same.

One solution to resolve this problem that we can do is to define a table to show the many-many relationships between the developer and the project. This table will contain the developer id corresponding to the project’s id, also known as joinTable, as follows:

Now, we can define: a developer can belong to many projects and a project can host many developers.

The Entity class of 2 tables developer and project, now can be defined with the @ManyToMany annotation as follows:

Entity Developer:

Entity Project:

We will put the @ManyToMany annotation along with declaring a Collection variable in both the Developer and Project entities, and to show the relationship many-many, we will use another annotation @JoinTable in one of these two entities. You can set @JoinTable annotation anywhere, if this annotation is in the @Developer entity, then in the Project entity you must declare the mappedBy attribute in the @ManyToMany annotation and vice versa.

In the @JoinTable annotation that we defined in the Developer entity, here we have three attributes that we need to declare:

  • name: it declares the name of the joinTable (that is developer_project table),
  • joinColumns: it is the name of the column in the joinTable that developer table will be foreign key to,
  • inverseJoinColumns: it is the name of the column in the joinTable that project table will be foreign key to.

OK, so we have defined the entity Developer and entity Project with @ManyToMany annotation to demonstrate the many-many relationships between developer and project tables. Now let’s take an example to see how it works.

I will add the configuration file for JPA, persistence.xml, located in the /src/main/resources/META-INF directory with the following contents:

Suppose you have the following data in your database:

Using @ManyToMany annotation in JPA

When running the following example:

The results will look like this:

Using @ManyToMany annotation in JPA

Add Comment