Creating Composite Keys in Hibernate 5

A composite key is a primary key composed of multiple columns.

When you are creating a composite key, your persistent class must override the equals() and hashCode() method. This is required for for Hibernate caching to work correctly. It must also implement the Serializable interface.

Best way to do this is to create a class with all your composite key fields, mark it as @Embeddable and then annotate a field of that class type with @Id in your entity class.

@Embeddable

public class MyCompositeID implements Serializable {

int field1;

int field2;

 

The entity is defined as follows:

@Entity

public class Sample {

@Id

MyCompositeID id;

 

Alternative approaches to create composite keys

Hibernate also provides some alternative approaches for implementing composite keys, but the above approach is more preferred.

You can use the @Id annotation over multiple fields.

@Entity

public class Employee {

@Id

Long department;

@Id

Long empId;

 

In a mapping file, you can use <composite-id> to define the object ID, which consists of two properties:

<hibernate-mapping>

<class name="Emplyee" table="Employee">

<composite-id>

<key-property name="department" type="long" column="DEPT" />

<key-property name=" empId " type="long" column=" EMPID "/>

</composite-id>

</class>

</hibernate-mapping>

 

To retrieve a specified object from the database using the load() or get() methods, you will need to pass a newly created Employee object with the key fields set.

Employee emp = new Employee();

//set key fields

Employee employee = (Employee) session.get(Employee.class, emp);

As Hibernate requires any ID class to implement the java.io.Serializable interface, your persistent class should implement Serializable interface.

 

Extracting the fields that form the id as a separate class

It might not be a good thing to pass the complete persistent class object to retrieve records. A better way is to extract the fields that form the ID as a separate class.

The fields will be still present in the persistent class, and the external class is created with the same ID property names and types as in the persistent class. You also need to specify this external class as @IdClass in the persistent class.

Example ID class:

public class EmployeeId implements Serializable {

Long department;

Long empId;

 

You then need to modify the persistent class to specify this new ID class using @IdClass annotation:

@Entity

@IdClass(EmployeeId.class)

public class Employee {

@Id

Long department;

@Id

Long empId;

 

The mapping definition should also be modified to use the ID class:

<hibernate-mapping>

<class name="Employee" table="employee">

<composite-id name="id" class=" EmployeeId">

<key-property name="department" type="string" column="DEPT" />

<key-property name="empId" type="string" column=" EMPID "/>

</composite-id>

</class>

</hibernate-mapping>

 

Now, to retrieve an Employee object, you can pass in an instance of EmployeeId type:

EmployeeId employeeId = new EmployeeId(1,101);

Employee employee = (Employee) session.get(Employee.class, employeeId);

 

For Hibernate caching to work correctly, you need to override the equals() and hashCode() methods of your custom ID class.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)