Home  >  Hibernate

Example of @MappedSuperclass in Hibernate

By Arvind Rai, May 20, 2013
@MappedSuperclass in hibernate supports entities to keep basic properties in a super class. The super class annotated by @MappedSuperclass will not behave as independent entity. It will not be associated with any database table. If we want to change the column name different from super class, we need to use @AttributeOverride. Find the example.
BasicData.java
package com.concretepage.persistence;
import java.io.Serializable;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@MappedSuperclass
public class BasicData implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name = "id")
	private int id;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="create_date")
	private Calendar createDate;

	public BasicData() {
	}
	public BasicData(int id, Calendar createDate) {
		this.id = id;
		this.createDate = createDate;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Calendar getCreateDate() {
		return createDate;
	}
        public void setCreateDate(Calendar createDate) {
		this.createDate = createDate;
	}
} 

Student.java
package com.concretepage.persistence;
import java.util.Calendar;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="student")
@AttributeOverride( name="createDate", column = @Column(name="std_entry_date") )
public class Student extends BasicData {
	private static final long serialVersionUID = 1L;

	@Column(name="f_name")
	private String firstName;
	
	@Column(name="age")
	private int age;
	public Student(){
		
	}
        public Student(int id,String firstName, int age,Calendar createDate){
		super(id,createDate);
		this.firstName=firstName;
		this.age=age;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
} 
hibernate.cfg.xml
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost:3306/hibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="com.concretepage.persistence.Student"/>             
   </session-factory>
</hibernate-configuration> 
HibernateUtil.java
package com.concretepage.util;
import java.util.Calendar;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.Person;
import com.concretepage.persistence.Student;
import com.concretepage.persistence.Teacher;
public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
	       static {
		 try {
		  concreteSessionFactory = new AnnotationConfiguration()
			.configure().buildSessionFactory();
		  } catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		  }
		}
		public static Session getSession()
				throws HibernateException {
			return concreteSessionFactory.openSession();
		}
		
		public static void main(String... args){
			Session session=getSession();
			session.beginTransaction();
			Student s = new Student(1,"Ram",25,Calendar.getInstance());
	    	        session.save(s);
			session.getTransaction().commit();
	        }
        }
} 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us