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), 

" EMP_NAME " VARCHAR2(255 CHAR), PRIMARY KEY(Role_ID, MenuItem_ID)

   );


  

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.  



RoleMenu.hbm.xml

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




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

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

   <composite-id>

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

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


   </key-property></key-property></composite-id>

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

 </property></class>

</hibernate-mapping>




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

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

    </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 . 




@Embeddable

public class RolesMenu {

    @Column(name = "Role_ID")

    private String roleID;


  @Column(name = "MenuItem_ID")

    private String menuItemID;


    //getter, setter methods

}





 @Entity

 @Table(name = "RolesMenuItems")

 public class RolesMenuItems {


     @EmbeddedId

     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 . 


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;

import java.io.Serializable;


@Embeddable

public class EmployeeIdentity implements Serializable {

    @NotNull

    @Size(max = 20)

    private String employeeId;


    @NotNull

    @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;

    }


    @Override

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

    }


    @Override

    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;


@Entity

@Table(name = "employees")

public class Employee {


    @EmbeddedId

    private EmployeeIdentity employeeIdentity;


    @NotNull

    @Size(max = 60)

    private String name;


    @NaturalId

    @NotNull

    @Email

    @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;

        this.name = name;

        this.email = 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.