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 RolesMenuItems . Which is table for describing the role of the employee . We have table
primary key . So first create RolesMenu pojo class . Which will have these primary keys.
. Now we will discuss how to write code for the composite key for these two columns.
CREATE TABLE " RolesMenuItems "
(
"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 RolesMenuItems.hbm.xml file for the pojo class as below.
RolesMenuItems.hbm.xml
Place the RoleMenu.hbm.xml file under the src/main/resources folder
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bhirkutisoft">
<class name=" RolesMenuItems " table=" RolesMenuItems ">
<composite-id>
<key-property name="RoleId" column="Role_ID" />
<key-property name=" menuItemID " column=" menuItem_ID " />
</composite-id>
<property name="empName" column="EMP_NAME" />
</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 composit 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)
}
No comments:
Add your comment