JPA Many to One 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 all, we know that the one of the properties of JPA entities is that they can have relationships. They are related with each other using associations. In JPA, these associations are implemented using attribute mappings.This article will deal with jpa many to one mapping.

Introduction:

In JPA there are four types of mappings.

  • Many to One
  • One to One
  • Many to Many
  • One to Many.

As an example we will see JPA Many to One mapping for entities Employee and Department . The condition is “Multiple Employee entities belong to One Department entity.”. i.e. There should be JPA Many to One mapping between these two entities.

In database, they are related such that.

  • There is a separate table called Department with primary key column iddepartment.
  • There is a separate table called Employee with primary key column idemployee.
  • Employee table contains a foreign key to Department table using column iddepartment.
  • This means Employee table has the ownership of Department table as it is referenced through the foreign key.

The database structure is as follows:

ManyToOneDatabase

Create Employee entity and Department entity as follows:

 Department.java

Notice the annotations used here

  • @TableGenerator and @GeneratedValue are used for automatic ID creation using jpa table generator.
  • @ManyToOne annotation specifies the JPA Many to One mapping relationship between Employee entity and Department entity. It is done by including a Department field in Employee class.
  • @JoinColumn annotation specifies the name of column for foreign key which relates employee to department in the database using name = "iddepartment" .

General Points to remember while creating JPA Many to One mapping:

  • The side having @JoinColumn annotation is called as the owning side of the relationship. Here, Employee is the owner.
  • The side which does not have @JoinColumn is the non-owning or inverse side. Here, Department is the inverse side.
  • @JoinColumn is always defined on the owning side of relationship.
  • The database table which has the foreign key i.e. the many side of  mapping  is the owning side in relationship.

Let us run a sample program to check our relationship mappings.

This will insert a record in database with employee details and assign the department to it using foreign key column. I hope this helps understand the concept of jpa many to one mapping. Please feel free ask questions and suggest any improvements.

References for further study :

Share Button

Prasad Kharkar

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.

12 thoughts on “JPA Many to One mapping

  • Pingback:JPA one to one mapping | theJavaGeek

  • Pingback:JPA one to many mapping | theJavaGeek

  • Pingback:JPA Inner Joins | theJavaGeek

  • Pingback:JPA JoinColumns Annotation Example | theJavaGeek

  • Pingback:JPA MapsId example | theJavaGeek

  • October 30, 2014 at 9:37 am
    Permalink

    In a unidirectional ManytoOne assocaition ,in above example,say we are having unidirectional relatina ship from department to employee,Can we use @joincolumn in deparment entity?ForeignKey is still in Employee table

    Reply
    • October 30, 2014 at 11:36 am
      Permalink

      Hi Reddy, foreign key will always be in the table which is the many side of association. If you keep the relationship such that many side is Department and one side is Employee, then foreign key for employee will be in Department table. It will mean one employee can belong to multiple departments.

      Reply
      • October 31, 2014 at 7:30 pm
        Permalink

        Thanks for response, 🙂

        When you say
        “foreign key will always be in the table which is the many side of association”

        It means according to Employee and Department Table relation

        Every row in EMployee table can hold one value in foreignkey

        Its not Possible in Department Table(One side of association),because every row in department table cannot hold more than one value in foregn key(DBMS basic COncept)

        Let me know if iam correct in the above point

        and my above question is many side still with Employee and one side still with Department

        Actually what i have done is while creating Employee table,i have set Foreign key column null means column accepts null value ,so in Entities instead of specifying join column in EMployee annotaton,i have specified join column in Deparmetn Entity,still association is same Many to one (Employee to Department).These are my findings

        Employeee — Department relationship

        join column in Department Entity

        Hibernate:
        insert
        into
        JPA.Department
        (name, id)
        values
        (?, ?)
        Hibernate:
        insert
        into
        JPA.Employee
        (ADDRESS_ID, MANAGER_ID, name, salary, startdate, id)
        values
        (?, ?, ?, ?, ?, ?)
        Hibernate:
        update
        JPA.Employee
        set
        DEPARTMENT_ID=?
        where
        id=?

        An Extra update statement

        join column in Employee Entity

        Hibernate:
        insert
        into
        JPA.Department
        (name, id)
        values
        (?, ?)
        Hibernate:
        insert
        into
        JPA.Employee
        (ADDRESS_ID, DEPARTMENT_ID, MANAGER_ID, name, salary, startdate, id)
        values
        (?, ?, ?, ?, ?, ?, ?)

        Reply
        • November 3, 2014 at 2:50 pm
          Permalink

          @JoinColumn is used to specify the name of column when it is used as foreign key in a table. So when you are using a foreign key in Employee table, then @JoinColumn should be used it Employee entity only. I will get a good understanding of the situation if you could post your entities here. I will look into the problem you are getting.

          Reply
  • November 17, 2014 at 1:41 pm
    Permalink

    what if I delete entry in department table,which is holding as foreign key in employee table…so while deleting would it automatically set to be null in emp. table or do I need to make it null explicitly ??

    Reply
    • November 25, 2014 at 4:58 pm
      Permalink

      Hi Prateek. I am sorry I didn’t see your comment earlier. Ideally it should throw an exception from database side as you are trying to delete an entity which has reference in other table. I will check and let you know to be sure.

      Reply

Leave a Reply

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