How  we    implement  composite  key   in   table  using annotation  and  xml configuration.

   Primary key --    This  is use for the table column

    To make sure  that  rows will unique. 

 Composite key --  When  we have   more then two   column  as primary key in the table known 

 as composite  key .   


 We  will   consider  an example  with a  table RolesMenu . Which   is table for describing the role  of the  employee .  In this table we two column  which   has primary  key .  Role_ID and 

MenuItem_ID .  Now   we  will  discuss how to 

Write   code for the  composite key  for these two  columns. 

CREATE TABLE " RolesMenu " 


        "Role_ID" VARCHAR2(255 CHAR), 

" MenuItem_ID " VARCHAR2(255 CHAR), 




First  we  will consider the  xml based configuration .  So  we  have RoleMenu 

Class  where  we  written  all columns  and getter setter for these column . The 

RoleMenu.hbm.xml  file  for  the pojo class  as  bleow.  


Place the RoleMenu.hbm.xml file under the src/main/resources folder

<hibernate-mapping package="com.bhirkutisoft">

 <class name=" RolesMenuItems " table=" RolesMenuItems ">


     <key-property column="Role_ID" name="RoleId">

     <key-property column=" menuItem_ID " name=" menuItemID ">


   <property column="EMP_NAME" name="empName">



 Here the   primary  key columns are   enclosed in the     <composite-id>

      --------- ----


 Now   we  can   see  how  we can code for  it  for  annotation based configuration .  @Embeddable  and      @EmbeddedId two  annotation 

 We   use  for   composite  key  column class . 


public class RolesMenu {

    @Column(name = "Role_ID")

    private String roleID;

  @Column(name = "MenuItem_ID")

    private String menuItemID;

    //getter, setter methods



 @Table(name = "RolesMenuItems")

 public class RolesMenuItems {


     private RolesMenu roleMenu;

      @Column(name = " EMP_NAME ")

      private String empName;


  /*setter getter methods */


 This   can be    understood  more easily  by another example  of the table Employee . 


company_id        VARCHAR(25)

employee_id       VARCHAR(25)

email             VARCHAR(30)

name              VARCHAR(50)

Phone_number      VARCHAR(50)


package com.example.jpa.model;

import javax.persistence.Embeddable;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Size;



public class EmployeeIdentity implements Serializable {


    @Size(max = 20)

    private String employeeId;


    @Size(max = 20)

    private String companyId;

    public EmployeeIdentity() {


    public EmployeeIdentity(String employeeId, String companyId) {

        this.employeeId = employeeId;

        this.companyId = companyId;


    public String getEmployeeId() {

        return employeeId;


    public void setEmployeeId(String employeeId) {

        this.employeeId = employeeId;


    public String getCompanyId() {

        return companyId;


    public void setCompanyId(String companyId) {

        this.companyId = companyId;



    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        EmployeeIdentity that = (EmployeeIdentity) o;

        if (!employeeId.equals(that.employeeId)) return false;

        return companyId.equals(that.companyId);



    public int hashCode() {

        int result = employeeId.hashCode();

        result = 31 * result + companyId.hashCode();

        return result;



2. Employee - Domain model

package com.example.jpa.model;

import org.hibernate.annotations.NaturalId;

import javax.persistence.Column;

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

import javax.persistence.Table;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Email;

import javax.validation.constraints.Size;


@Table(name = "employees")

public class Employee {


    private EmployeeIdentity employeeIdentity;


    @Size(max = 60)

    private String name;




    @Size(max = 60)

    private String email;

    @Size(max = 15)

    @Column(name = "phone_number", unique = true)

    private String phoneNumber;

    public Employee() {


    public Employee(EmployeeIdentity employeeIdentity, String name, String email, String phoneNumber) {

        this.employeeIdentity = employeeIdentity; = name; = email;

        this.phoneNumber = phoneNumber;


    // Getters and Setters (Omitted for brevity)


Reviewed by Mukesh Jha on 3:25 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.