Hibernate Composite Primary Key

Hibernate Composite Primary Key

A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is known as a composite primary key or a composite key.

Composite keys are a group of columns in the database, whose values together make a unique value.

We can create a composite key in two ways:

  • Using an XML file
  • Using annotations

Using an XML File

We can create a composite key using an XML mapping file. In that mapping file, we are using a <composite-id>…<composite-id/> tag to declare a composite key. All the primary key objects of the POJO class are defined inside the composite key tag.

Syntax of <composite-id>


      
      
  

With Hibernate 5 (version 5 and above), composite key using annotations are more popular than an XML file.  Hence, we are going to create an example of a composite key using annotation.

Using annotations

We can create a composite key using @Embeddable annotation. It is used in that POJO class which contains the information of all the primary keys.

@Embeddable- It specifies a class whose objects are stored as an intrinsic part of an owing entity. And also, it shares the identity of the entity. Every field of the embedded object is mapped with the database table. It is available in javax.persistence package. 

In order to define composite keys, we should implement the given rules:

  1. The composite key class should be public.
  2. The composite key class must implement the Serializable interface.
  3. The class must have a constructor.
  4. The class should have equals()  and hashCode() methods.

Syntax of @Embeddable

@Embeddable
 public class Ck_accId implements Serializable{
             private int user_id;
             private int acc_id;
    } 

Example of Composite Key using annotation

Here, we are going to create an example of a composite key. In this example, we are taking two classes, Account.java and Ck_accId.java. The Account.java is a pure POJO class, whereas Ck_accId.java is a composite key class.

Following are the steps to create a composite key:

Account.java

import javax.persistence.Entity;
 import javax.persistence.Id;
 @Entity
 public class Account {
             Ck_accId ck_accid;
             private int acc_bal;
             @Id 
               public Ck_accId getCk_accid() {
                         return ck_accid;
             }
               public void setCk_accid(Ck_accId ck_accid) {
                         this.ck_accid = ck_accid;
             }
               public int getAcc_bal() { 
                         return acc_bal;
             }
               public void setAcc_bal(int acc_bal) {
                         this.acc_bal = acc_bal;
             }
     } 

The Account.java contains an object of Ck_accId class. It also contains a variable named acc_bal.

Ck_accId.java

import java.io.Serializable;
 import javax.persistence.Embeddable;
 @Embeddable
 public class Ck_accId implements Serializable{
             private int user_id;
             private int acc_id;
             public Ck_accId(int user_id, int acc_id) {
                         super(); 
                         this.user_id = user_id;
                         this.acc_id = acc_id;
             }
             public int getUser_id() {
                         return user_id;
             }
             public void setUser_id(int user_id) { 
                         this.user_id = user_id;
             }
             public int getAcc_id() {
                         return acc_id;
             }
             public void setAcc_id(int acc_id) { 
                         this.acc_id = acc_id;
             }
             @Override
             public int hashCode() {
                         final int prime = 31;
                         int result = 1;
                         result = prime * result + acc_id; 
                         result = prime * result + user_id;
                         return result;
             }
             @Override
             public boolean equals(Object obj) {
                         if (this == obj)
                                     return true;
                         if (obj == null) 
                                     return false;
                         if (getClass() != obj.getClass())
                                     return false;
                         Ck_accId other = (Ck_accId) obj;
                         if (acc_id != other.acc_id)
                                     return false; 
                         if (user_id != other.user_id)
                                     return false;
                         return true;
             }
   } 

The Ck_accId.java is an Embedded class. It contains two primary key variables, user_id, and acc_id.

hibernate.cfg.xml

  
   
   
       
         create  
         org.hibernate.dialect.MySQL5Dialect 
         jdbc:mysql://localhost:3306/test2
         com.mysql.jdbc.Driver
         root  
         root   
         
       
       
    

The hibernate.cfg.xml contains the information of the database and the mapping class (com.app.Composite_key.Account).

App.java

import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 public class App 
 {
     public static void main( String[] args )
     {
        Configuration cfg = new Configuration(); 
        cfg.configure("hibernate.cfg.xml");
        SessionFactory fact= cfg.buildSessionFactory();
        Session sess = fact.openSession();
        sess.beginTransaction();
        Ck_accId id1= new Ck_accId(10, 1016);
         Account acc= new Account();
         acc.setCk_accid(id1);
         acc.setAcc_bal(8100); 
        Ck_accId id2= new Ck_accId(11, 1020);
        Account acc2 = new Account();
        acc2.setCk_accid(id2);
        acc2.setAcc_bal(7200);
        sess.save(acc); 
        sess.save(acc2);
        sess.getTransaction().commit();
        System.out.println("created");
        sess.close();
     }
 } 

The App.java is the main class, contains the main() method.

OUTPUT

contains the main() method

Database Table- Account



ADVERTISEMENT
ADVERTISEMENT