Wednesday , June 28 2017
Home / Hibernate / Hibernate Composite Key Mapping Example

Hibernate Composite Key Mapping Example

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, 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`) ) ENGINE=InnoDB AUTO_INCREMENT=1

Project Structure :

Hibernate Composite Key Example

Required Dependencies :

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

HibernateUtility :

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(;
        return sessionFactory;


Hibernate POJO:

package com.onlinetutorialspoint.beans;


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 :


<?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 :

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


Download Example

About chandrashekhar

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

Check Also

Hibernate Native SQL Query Example

In this tutorials, we are going to learn about Hibernate Native SQL with example. Hibernate …

Leave a Reply

Your email address will not be published. Required fields are marked *