JPA many to many mapping

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, In previous section we have seen about one to many mapping. This article will deal with jpa many to many mapping.

 

JPA Many to many mapping:

Consider a scenario that employees can belong to multiple projects. i.e. Multiple employees can work on multiple projects. Create a database such that.

  • Employee table has the primary key idemployee
  • Project table has primary key idproject
  • Employee_Project table which has columns as foreign keys to Employee and Project tables.
  • Employee_Project table is a join table which stores the information about the mapping of projects to employees.

Database diagram will be

Relation between Employee and Project tables

Relation between Employee and Project tables

Employee Entity

 Project Entity

 Points to notice:

  • @TableGenerator and @GeneratedValue are used for automatic ID creation using jpa table generator.
  • @ManyToMany annotation specifies the relationship between Employee and Project entities.
  • @JoinTable specifies the name of the table to use as join table jpa many to many mapping between Employee and Project using name = “employee_project”. This is done because there is no way to determine the ownership of a jpa many to many mapping as the database tables do not contain foreign keys to reference to other table.
  • @JoinColumn specifies the name of column that will refer to the Entity to be considered as owner of the association while @inverseJoinColumn specifies the name of inverse side of relationship. (You can choose any side to be considered as owner. Just make sure those sides in relationship). In our case we have chosen Employee as the owner so @JoinColumn refers to idemployee column in join table employee_project and @InverseJoinColumn refers to idproject which is inverse side of jpa many to many mapping.
  • @ManyToMany annotation in Project entity shows inverse relationship hence it uses mappedBy=projects to refer to the field in Employee entity.

 

Let us test it out using

This will insert records into the join table showing the relationship between each employee and projects.

I hope this article helped understand jpa many to many mapping. Please feel free to provide feedback so that I can serve better 🙂

 

References for further study :

Share Button

3 comments for “JPA many to many mapping

  1. Reddy
    November 5, 2014 at 9:14 pm

    Hi Prasad,

    Employee and Project are 2 Entities,and for Employee to be the owner of association,we use mappedby annotation in Project Entity as show below

    @ManyToMany(mappedBy = “projects”, cascade = CascadeType.PERSIST)
    private List employees;

    this means , Employe Entity will be owner of the assocuation

    In Employee Entity

    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name = “employee_project”, joinColumns = @JoinColumn(name = “idemployee”), inverseJoinColumns = @JoinColumn(name = “idproject”))
    private List projects;

    In JoinTable annotation, what is purpose of inverseJoinColumn,?

    –> joinColumns = @JoinColumn(name = “idemployee”),
    this means that for the Employee–Project associaton, idemployee column will treated as foreignkey column

    then what is need to use
    inverseJoinColumns = @JoinColumn(name = “idproject”)
    ?

    Thanks in Advance
    Reddy

    • November 6, 2014 at 9:33 am

      Hi Reddy, mappedBy annotation is used to show the bidirectional relationship between Employee and Project. Here we have considered Employee as owning side hence annotated projects field with @JoinTable. joinColumns specifies foreign key columns for owning side of relationship i.e. Employee while inverseJoiColumns specifies foreign key columns for non-owning side of relationship i.e. Project. @JoiTable, joinColumns and inverseJoinColumns are optional and if not used, default names will be used. As we want to use the existing database column names, we have specified them explicitly. You can read about inverseJoinColumns in official oracle javadoc

      I hope this answers your question.

  2. Reddy
    November 6, 2014 at 12:26 pm

    Thanks Prasad,for Quick response :-).
    Really appreciate your patience for the explanation.

    Just few more doubts

    —> joinColumns specifies foreign key columns for owning side of relationship i.e. Employee . Does it mean,
    For Employee table,,idemployee column of employee_project table is foreign key and for Project table,, idproject column of employee_project is foreign key?

    I mis understood it in reverse way as idproject column of employee_project is foreign key to Employee table and idemployee column of employee_project table is foreign key to Project table.

    So please correct me where i went wrong.

    2)And other one is ,mappedby annotation lets jpa provider know that in a bi-directional association,other side of the association is the owner of the relationship and hence responsible for updating the relationship that is foreign key column.

    is there anything that iam missing still in understanding the below statement
    “mappedBy annotation is used to show the bidirectional relationship between Employee and Project”

    What i understood is JPA/Hibernate doesnt automatically detect bidirectional relation we have to explictly set them in our code like for example
    employee.setProject
    project.setEmployee.

    ThanksinAdvance
    Reddy

Leave a Reply

Your email address will not be published. Required fields are marked *