Home  >  Hibernate

Hibernate Inheritance: InheritanceType.SINGLE_TABLE

By Arvind Rai, May 19, 2013
InheritanceType.SINGLE_TABLE works by single table. There will be one super entity and one derived entity and when we insert data for any entity, data will be inserted into single table. Rows are distinguished by @DiscriminatorValue. Association with single table of super and derived entity improves data fetching performance. Syntax is
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
	    name="personType",
	    discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("Person")
 





Person.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
	    name="personType",
	    discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("Person")
@Table(name="person")

public class Person implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Column(name="id")
  private int id;

  @Column(name="name")
  private String name;
  
  @Column(name="age")
  private int age;
  public Person(){
	  
  }
  public Person(int id,String name, int age){
	  this.id=id;
	  this.name=name;
	  this.age=age;
  }
  public int getId() {
		return id;
  }
  public void setId(int id) {
		this.id = id;
  }
  public String getName() {
		return name;
  }
  public void setName(String name) {
		this.name = name;
  }
  public int getAge() {
		return age;
  }
  public void setAge(int age) {
		this.age = age;
  } 
}
 


Student.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="student")
@DiscriminatorValue("Student")
public class Student extends Person implements Serializable {
	private static final long serialVersionUID = 1L;
	@Column(name="class_name")
	private String className;
	
	@Column(name="college_name")
	private String college;
	public Student(){
	}
    public Student(int id,String name, int age,
	String className,String college){
		super(id,name,age);
		this.className=className;
		this.college=college;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
}
 


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.Person"/>
    <mapping class="com.concretepage.persistence.Student"/>
                
   </session-factory>
</hibernate-configuration>
 


HibernateUtil.java
 package com.concretepage.util;
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;
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();
			Person p=new Person(1,"Ram",30);
	    	session.save(p);
			Student s=new Student(2,"Ram",30,"MCA","IET");
			session.flush();
			session.clear();
			session.save(s);
			session.getTransaction().commit();
		}
	}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us