Hibernate – @MappedSuperclass How to override column identifier

In this tutorial I will show how to override an attribute from mapped super class.

1. Sql Script

Use the following Sql Script for creating table.

create table User(
    ID int(10) primary key NOT NULL AUTO_INCREMENT,
    Name varchar(50));

2. Pojo

Now create GenericId and User Class as following.

GenericId.java : an abstract class, annotated with @MappedSuperclass that have id column

package com.kruders.model.bean;

import javax.persistence.MappedSuperclass;

public abstract class GenericID {
	private Integer id;

	public Integer getId() {
		return id;

	public void setId(Integer id) {
		this.id = id;

User.java : an @Entity class that extends the super class and overriding the getters of the id.

package com.kruders.model.bean;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "User")
@AttributeOverride(name = "id", column = @Column(name = "ID"))
public class User extends GenericID{
    private Integer Id;
    private String name;
    public Integer getId() {
		return Id;
	public void setId(Integer id) {
		Id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;

3. Hibernate Configuration File

Now create the hibernate configuration file and add all the mapping files.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="connection.pool_size">1</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="show_sql">true</property>
	<mapping class="com.kruders.model.bean.Employee" />
	<mapping class="com.kruders.model.bean.EmployeeDetail" />

4. Hibernate Utility Class

Now create HibernateUtil class. The HibernateUtil class helps in creating the SessionFactory from the Hibernate configuration file. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances. A org.hibernate.Session represents a single-threaded unit of work. The org.hibernate.SessionFactory is a thread-safe global object that is instantiated once.

package com.kruders.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;

5. Run Program

Create Main.java class that adds a record and run it as Java Application

package com.kruders.core;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.kruders.model.bean.User;
import com.kruders.util.HibernateUtil;
public class Main {
    public static void main(String args[]) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            User user = new User();
        }catch (HibernateException e) {
        } finally {
You can download the source code of this example here.

One Response to Hibernate – @MappedSuperclass How to override column identifier

  1. Fabio Ebner March 8, 2013 at 6:55 pm #

    Hey man.. did you know how i override only one annotation??

    public class IndiceBase {
    @Basic(optional = false)
    @Column(name = “nm_parte”)
    private String nmParte;
    //getters and setters

    public class indice extends IndiceBase{

    @Column(name = “nm_parte”)
    private String nmParte;


    it is possible??


Leave a Reply