JPA @IdClass example

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)

We know that a database table can contain compound primary keys. i.e. the primary key is composed of more than one columns. Compound primary keys can be created in JPA in two ways, using @IdClass and @EmbeddedId annotation. Here, we will see JPA @IdClass example.

JPA @IdClass example:

Consider Employee entity such that.

  • The primary key of Employee table is composed of two columns.
  • The columns are idEmployee and branchName
  • The combination of idEmployee and branchName collectively makes the primary key.

In database, Employee table is as depicted.

EmployeeEntity

We can create compound primary keys in JPA using @IdClass annotation and corresponding java id class. This is what javadoc says about it

Specifies a composite primary key class that is mapped to multiple fields or properties of the entity.

The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.

Let us consider code below.

 

Note that for this JPA @IdClass example, we have specified @IdClass(EmployeeId.class) for the entity; meaning it should use specified class objects as primary key.

Some points to note in this JPA @IdClass example:

  • The primary key class, in this case EmployeeId must contain fields that match the fields annotated with @Id in entity class.
  • So, primary class contains fields branchName and idEmployee as they are annotated with @Id  in Employee entity.
  • There are no setter methods for EmployeeId class for obvious reason that primary key values should not be changed.
  • We should override equals() and hashcode()  as these primary key objects need to be identified uniquely and we can use them in collections those use hashing.

Now we can check this jpa @IdClass example by trying to find an Employee with a specific branchName and idEmployee.

Data in Employee table is as below.

1 pune Prasad Kharkar 100000

Let us try to find entity using jpa code.

This find the employee from database and prints its information.

First name = Prasad Last Name = Kharkar salary = 100000

I hope this JPA @IdClass example helped understand the concept of coumpound primary key in jpa. In next article we will see how this can be done alternatively.

 

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.

21 thoughts on “JPA @IdClass example

  • Pingback:JPA @EmbeddedId example | theJavaGeek

  • November 1, 2014 at 10:51 pm
    Permalink

    hi,

    Can you please provide sample example for save data in composite key table with auto generation..

    Thanks in Advance.

    Reply
    • November 3, 2014 at 12:17 pm
      Permalink

      I didn’t get what you meant by composite key table. Could you please elaborate?

      Reply
    • December 3, 2016 at 12:53 am
      Permalink

      very good post, It ssaved my time.

      Reply
  • January 20, 2015 at 3:40 am
    Permalink

    Can you explain how you’ll use the Primary key (idClass) in the HashMap for the above scenario?

    for example

    map.put(employee,salary);

    Here employee is the key, since the id class is the one which has the equals and hash code implementation how can we use the idClass in this scenario?

    Reply
    • January 20, 2015 at 2:39 pm
      Permalink

      As far as I understand, EmployeeId class guarantees the uniqueness, so we should do something like map.put(employeeId, salary). Your question is interesting and I will try to try it out. Please give some time 🙂

      Reply
  • August 25, 2015 at 2:50 pm
    Permalink

    Hi,

    Thanks for sharing your knowledge.

    How can I do if I want that the property branchName has in database the column name for example MY_BRANCH_NAME instead of branchName without modifying the java property branchName ?

    Thanks a lot

    Reply
  • November 5, 2015 at 1:23 am
    Permalink

    Hi Prasad,

    Nice post. I have one question, if my entity class having composite primary key and if I want to fetch the data using only one field not by using Id class instance. Will it create problem ?

    Reply
    • November 5, 2015 at 9:01 am
      Permalink

      Hi Atul, thank you for your comment. How are you fetching data with only one field? Could you please explain?

      Reply
  • April 24, 2016 at 6:45 am
    Permalink

    what if it is like this in film table film_id is primary key and in category table category_id is primary key and in actor table actor_id is primary key and we have two other tables containing film_id and category_id called film_category and film_actor table having film_id and actor_id

    Reply
  • May 26, 2016 at 8:10 pm
    Permalink

    How persistence work in Spring Data Rest for composite primary key?

    Reply
  • September 20, 2016 at 7:00 pm
    Permalink

    Thanks bro. This post helped me to solved my query.

    Reply
  • November 26, 2016 at 3:31 pm
    Permalink

    Hi.
    Great post. I wonder how to find all employees with one branchName or all branchNames for one employee?

    Thanks

    Reply
  • March 18, 2017 at 2:12 pm
    Permalink

    Hi ,

    Can u please explain how to persist instead of reteriving the data

    Reply
    • March 20, 2017 at 10:22 am
      Permalink

      persisting data is not different from any other entity persistence. Could you please explain your scenario a bit more?

      Reply
  • July 1, 2017 at 12:09 am
    Permalink

    Hi Prasad,

    Nice post, it was very helpful. Thanks

    Reply
  • July 7, 2017 at 10:35 am
    Permalink

    why did u say that primary key values should not change?
    i have a scenario –

    parent entity (one to manY)
    child many to one

    parent fk- child pk (composite key).

    now i want to update the child composite key. I am unable to do it. I have a dirty way of using @prepersist in child but looking for better options

    Reply
  • October 24, 2017 at 1:00 pm
    Permalink

    Hi Prasad,

    I have a doubt, the implementation u have given works with Oracle, this doesn’t work with MySQL.
    Can you please suggest something where i can use auto-generated values in a composite key in MySQL?

    Reply
    • October 25, 2017 at 9:24 am
      Permalink

      could you please let me know what is the problem you are facing? I have used MySQL for all of my jpa tutorials and not oracle.

      Reply
  • Pingback:Not a managed type creating and composite id with @IdClass – program faq

Leave a Reply

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