July 25, 2014

Implementing composite primary key with JPA and Hibernate

Implementing composite key is different from the normal primary key , which I have explained in my previous post How to implement primary key with JPA and Hibernate .In this example we are going to learn how to create a compound primary key.

Here let’s take an example table  ACCOUNT with three attributes

[Emp_id]

[Account_id]

[Balance].

imagine a situation where we need a combination of Emp_id and Account_id to make up our prmary key.
For this , we will create a separate class with the attributes with which we want to create a compound key ie,  Emp_id and Account_id

The above compound key class need special implementation

1)  The compound key class must implement serializable interface.

2)  Annotate the class with embeddable.

3)  All the getters and setters.

4) You can create the constructor, Which is not mandatory.

Now let’s create the  class for Object relational mapping. Instead of  getter/setter methods of the three data , we will create one object for the compound key class and the other attribute Balance.

 

Here you can notice that we have used  @Id – for creating id we have created the object of the class CompoundKey

Hibernate Client code

 

Comments

  1. Is this entity getting updated as well?
    Using a similar configuration, I am able to save into the database, but am unable to update it.

    • You can do update also using the same way explained in the example . Please try this code

      String updateBalanceQuery=”update AccountVO accountvo set accountvo.balance=500 where accountvo.compoundKey.emp_id=:empID”;
      Query query=session.createQuery(updateBalanceQuery);
      query.setString(“empID”, “100848″);
      query.executeUpdate();

  2. This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  3. Chen Bo says:

    Thank you for the post. It works.

    However, I have a question about the annotation mapping.

    In class AccountVO balance field, we have annotation @Column(name=”Balance”) to clearly map balance to the corresponding table column. However, for the CompoundKey, we do not have similar annotations for the compound key fields. How does Hibernate know which column in the database table to map the key fields to?

    Thanks.

  4. Thanks dude!!!
    Great and Simple tutotial…

    Many Thanks,
    Ali

  5. Great……i understood abt wt u have implemented but wt my question is how to implement auto increment for composite key.

    Thanks
    chandra

  6. Thanks

  7. what happens if you are creating the account or employee. the composite key wouldn’t have the account id available at that time, right? how does that work?

  8. Thank you very much for sharing this code. It helped me in solving the issue :)

  9. Thank you, this helped. :)

  10. Peter Sarazin says:

    This got me 90% of the way in my application. My class attribute names do not match my database column names, so to make it fully work I just had to add @Column annotations to my equivalent of the emp_id and account_id attributes in the CompoundKey class implementation.

Leave a Reply