Home  >  Hibernate

Hibernate Inheritance: InheritanceType.TABLE_PER_CLASS

By Arvind Rai, May 19, 2013
InheritanceType.TABLE_PER_CLASS is the inheritance strategy in which one entity will extend another entity and the derived entity will contain the property of super entity. In the example we have two entities Person and Student. Student entity will override Person entity. Syntax is
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 





Person.java
package com.concretepage.persistence;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@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.Entity;
@Entity
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();
			Person p=new Person(1,"Ram",30);
			session.save(p);
			Student s=new Student(1,"Ram",30,"MCA","IET");
			session.flush();
			session.clear();
			session.save(s);
			session.beginTransaction();
		
			session.getTransaction().commit();
		}
	}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us