Hibernate Composite Key Mapping Example

By | 2018-03-19T05:47:15+05:30 December 28th, 2015|Hibernate|

In the context of relational databases, a composite key is a combination of two or more columns in a table that can be used to make the uniqueness of a table row. In this tutorials, we are going to implement how to define a hibernate composite key, and how to map the database composite primary keys in a hibernate mapping. Here is the example for Hibernate composite key:

Hibernate Composite Key Mapping :

Lets create a database table with composite primary key.

CREATE TABLE `student` (
`studentid` int(11) NOT NULL AUTO_INCREMENT,
`courceid` int(11) NOT NULL,
`studentname` varchar(50) DEFAULT NULL,
`studentAddress` varchar(50) DEFAULT NULL,
PRIMARY KEY (`studentid`,`courceid`)

Project Structure : Hibernate Composite Key Example Required Dependencies :

            <!-- Hibernate -->
            <!-- MySQL Driver -->

HibernateUtility : HibernateUitl.java

package com.onlinetutorialspoint.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private HibernateUtil() {


    private static SessionFactory sessionFactory;

    public static synchronized SessionFactory getInstnce() {

        if (sessionFactory == null) {
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
            sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;


Hibernate POJO: Student.java

package com.onlinetutorialspoint.beans;

import java.io.Serializable;

public class Student implements Serializable {

    private static final long serialVersionUID = 1L;
    private int studentId;
    private String studentName;
    private String studentAddress;
    private int courceId;

    public int getStudentId() {
        return studentId;

    public void setStudentId(int studentId) {
        this.studentId = studentId;

    public String getStudentName() {
        return studentName;

    public void setStudentName(String studentName) {
        this.studentName = studentName;

    public String getStudentAddress() {
        return studentAddress;

    public void setStudentAddress(String studentAddress) {
        this.studentAddress = studentAddress;

    public int getCourceId() {
        return courceId;

    public void setCourceId(int courceId) {
        this.courceId = courceId;


To represent the composite key in hibernate, the pojo class should implement the Serializable interface. Otherwise hibernate will throw org.hibernate.MappingException like "Composite-id class must implement Serializable"

Hibernate Mapping File : student.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 <class name="com.onlinetutorialspoint.beans.Student" table="student">
 <key-property name="studentId" column="studentid" />
 <key-property name="courceId" column="courceid" />
 <property name="studentName" />
 <property name="studentAddress" />

To represent the composite keys, hibernate provides <key-property> tag under the <composite-id> tag.
We can represent each primary key with <key-property> tag, so that all primary keys will be binded with the <composite-id> tag.

Lets Run the Application : Main.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.onlinetutorialspoint.beans.Student;
import com.onlinetutorialspoint.util.HibernateUtil;

public class Main {
    public static void main(String args[]) {
        SessionFactory sessionFactory = HibernateUtil.getInstnce();
        Session session = sessionFactory.openSession();
        Student student = new Student();
        Transaction tx = session.beginTransaction();


Hibernate: insert into student (studentName, studentAddress, studentid, courceid) values (?, ?, ?, ?)

  Hibernate composite key table Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment