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
|
import java.io.Serializable; import javax.persistence.Embeddable; @Embeddable public class CompoundKey implements Serializable{ private String emp_id; private String account_id; public CompoundKey(String emp_id,String account_id) { this.emp_id=emp_id; this.account_id=account_id; } public String getAccount_id() { return account_id; } public void setAccount_id(String account_id) { this.account_id = account_id; } public String getEmp_id() { return emp_id; } public void setEmp_id(String emp_id) { this.emp_id = emp_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.
|
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table @Entity @Table(name=”ACCOUNT”) public class AccountVO { private CompoundKey compoundKey; private int balance; @Id public CompoundKey getCompoundKey() { return compoundKey; } public void setCompoundKey(CompoundKey compoundKey) { this.compoundKey = compoundKey; } @Column(name=”Balance”) public int getBalance() { return balance; } public void setBalance(int balance) { this.balance = 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
|
package compoundPrimarykey; import java.util.Iterator; import org.hibernate.Criteria; public class testHibernate { AnnotationConfiguration config=new AnnotationConfiguration(); |
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();
Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.
Great information! I’ve been looking for something like this for a while now. Thanks!
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!
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.
I want to put @GeneratedValue for one of id, please explain the scenario
Thanks dude!!!
Great and Simple tutotial…
Many Thanks,
Ali
Great……i understood abt wt u have implemented but wt my question is how to implement auto increment for composite key.
Thanks
chandra
Thanks
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?
Thank you very much for sharing this code. It helped me in solving the issue
Thank you, this helped.
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.