Hibernate Mapping Entity with multiple primary key

Today i want to share about Hibernate Mapping Entity with multiple primary key. For some reason we need to add multiple key in our database table, for example: you have table S_PARAMETER that will save all updatable parameter in your system and for some reason(easy read, clean code) you nedd param_group, and param_id field to get the value. And how hibernate mapping can achieve that?

First, the table structure is like this:

Hibernate Mapping Entity with multiple primary key

And you need to create entity class to map table datas into your class domain/pojo.
Because Hibernate only accept one ID in your pojo class, so in this post, i will use Embeddable class that will save the multiple key. Here it is:

package com.didikhari.model;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

@Table(name = “S_PARAMETER”)
public class Parameter implements Serializable {
private static final long serialVersionUID = 1L;
@AttributeOverride(name=”paramGroup”, column = @Column(name = “PARAM_GROUP”)),
@AttributeOverride(name=”paramId”, column = @Column(name = “PARAM_ID”))
private ParamKey paramKey;
@Column(name = “DESCRIPTION”)
private String description;
@Column(name = “VALUE”)
private String value;
@Column(name = “ORDINAL”)
private int ordinal;

public String getParamGroup(){
return paramKey.getParamGroup();

public String getParamId(){
return paramKey.getParamId();

public String getDescription() {
return description;
public void setDescription(String description) {
this.description = description;
public String getValue() {
return value;
public void setValue(String value) {
this.value = value;
public int getOrdinal() {
return ordinal;
public void setOrdinal(int ordinal) {
this.ordinal = ordinal;

public ParamKey getParamKey() {
return paramKey;

public void setParamKey(ParamKey paramKey) {
this.paramKey = paramKey;

class ParamKey implements Serializable{
private static final long serialVersionUID = 1L;
private String paramGroup;
private String paramId;

public String getParamGroup() {
return paramGroup;
public void setParamGroup(String paramGroup) {
this.paramGroup = paramGroup;
public String getParamId() {
return paramId;
public void setParamId(String paramId) {
this.paramId = paramId;


We need to use AttributeOverrides annotation, for map the ParamKey properties (paramGroup and paramId) to the database table column (PARAM_GROUP, PARAM_ID).
Thats all we need to do to make hibernate mapping work for mutiple key field.
Hope this post about Hibernate Mapping Entity with multiple primary key


Hi! i’m Didik Hari, I am Backend Developer, System Integration and Web Developer mostly using Java. I use jPOS for ISO 8583 integration format, and Spring to create Rest API.

Leave a Reply

Your email address will not be published.