• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Implementing composite primary key with JPA and Hibernate

January 13, 2011 By j2eereference 16 Comments

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package compoundPrimarykey;
 
import java.util.Iterator;
import java.util.List;
 
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
 
public class testHibernate {
public static void main(String[] args) {
 
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(AccountVO.class);
config.addAnnotatedClass(CompoundKey.class);
SessionFactory sessionFactory = config.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx=session.beginTransaction();
CompoundKey compoundKey=new CompoundKey("100848","100");
AccountVO accountVO=new AccountVO();
accountVO.setCompoundKey(compoundKey);
accountVO.setBalance(100000);
session.save(accountVO);
Criteria crt=session.createCriteria(AccountVO.class);
List list=crt.list();
for (Iterator ite = list.iterator(); ite.hasNext();)
{
AccountVO accountVO2= (AccountVO) ite.next();
System.out.println("Account  Balance  ="+accountVO2.getBalance());
CompoundKey compoundKey2=accountVO2.getCompoundKey();
System.out.println("Emp id  ="+compoundKey2.getEmp_id());
System.out.println("Account  id  ="+compoundKey2.getAccount_id());
}
tx.commit();
}
}

 

Related Posts

  • First level caching in Hibernate
  • Hibernate – Query By Criteria
  • Hibernate Query Language
  • Many to Many mapping in hibernate
  • One To Many mapping using Hibernate
  • one to one mapping using Hibernate
  • How to implement primary key with JPA and Hibernate
  • What is Hibernate?

Filed Under: Hibernate

Reader Interactions

Comments

  1. Sumit says

    February 10, 2011 at 5:21 pm

    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.

    Reply
    • bkrakesh says

      February 10, 2011 at 6:17 pm

      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();

      Reply
      • Talia Frump says

        May 28, 2011 at 6:15 pm

        Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.

        Reply
      • Vernell Valdovino says

        May 29, 2011 at 9:53 pm

        Great information! I’ve been looking for something like this for a while now. Thanks!

        Reply
  2. Kenya Nevarrez says

    May 29, 2011 at 10:03 pm

    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!

    Reply
  3. Chen Bo says

    July 19, 2011 at 3:26 pm

    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.

    Reply
    • dhanasekhar says

      July 26, 2011 at 3:36 pm

      I want to put @GeneratedValue for one of id, please explain the scenario

      Reply
  4. Ali says

    February 6, 2012 at 3:02 pm

    Thanks dude!!!
    Great and Simple tutotial…

    Many Thanks,
    Ali

    Reply
  5. chandra says

    March 13, 2012 at 9:30 am

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

    Thanks
    chandra

    Reply
  6. Mansoor says

    March 25, 2012 at 8:18 pm

    Thanks

    Reply
  7. Sunil says

    June 20, 2012 at 4:20 am

    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?

    Reply
  8. Best of Android says

    August 7, 2012 at 5:55 pm

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

    Reply
  9. Tomek says

    November 28, 2012 at 2:54 pm

    Thank you, this helped. 🙂

    Reply
  10. Peter Sarazin says

    December 28, 2012 at 3:51 am

    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.

    Reply
  11. totanagouda says

    December 11, 2014 at 10:22 am

    good post

    Reply

Trackbacks

  1. Composite Key reference | snippetjournal says:
    May 8, 2015 at 3:52 pm

    […] Implementing composite primary key with JPA and Hibernate […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.