Home  >  Hibernate

Hibernate Inheritance: InheritanceType.JOINED

By Arvind Rai, May 19, 2013
In Hibernate Annotation, @Inheritance works for inheritance of entities. We will understand here inheritance type joined which is denoted by InheritanceType.JOINED. The nature of InheritanceType.JOINED is that the properties of super entity will be inherited by derived entity. And in the database the columns which super table contains will not be available in sub table. The super and derived entity will be associated by @PrimaryKeyJoinColumn. In our example I will show that I have table person will be extended by student and teacher. The syntax of joined inheritance is
 @Inheritance(strategy=InheritanceType.JOINED)
 
And the derived entity and super entity will be associated by
 @PrimaryKeyJoinColumn
 





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.JOINED)
@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.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="student")
@PrimaryKeyJoinColumn(name="id")
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;
	}
}
 


Teacher.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@PrimaryKeyJoinColumn(name="id")
@Table(name="teacher")
public class Teacher extends Person {
	private static final long serialVersionUID = 1L;
	
	@Column(name="qualification")
	private String qualification;
	
	@Column(name="designation")
	private String designation;
	public Teacher(){
	}
    public Teacher(int id,String name, int age,String qualification,String designation){
    	super(id,name,age);
    	this.qualification=qualification;
    	this.designation=designation;
	}
    public String getQualification() {
		return qualification;
	}
	public void setQualification(String qualification) {
		this.qualification = qualification;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
}
 


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"/>
    <mapping class="com.concretepage.persistence.Teacher"/>
                
   </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;
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",30,"MCA","IET");
			session.flush();
			session.clear();
			session.save(s);
			Teacher t=new Teacher(2,"Shyam",28,"PHD","professor");
	    	session.flush();
	    	session.clear();
	    	session.save(t);
			session.getTransaction().commit();
		}
	}
 
Output

Example of InheritanceType.JOINED in Hibernate
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us