Hibernate – Inheritance Mapping

Suppose that there are three classes called Project, Module and Task.
Module class extends Project class.
Task class extends Module class.

UML class diagram of this multilevel inheritance application is shown below.

UML Class Diagram

There are three types of strategies for inheritance mapping.
* SINGLE_TABLE (Default)
* JOINED
* TABLE_PER_CLASS

Now let’s examine how to use each strategy.

SINGLE_TABLE strategy
The default strategy is SINGLE_TABLE.You don’t need to write an annotation for inheritance strategy if your strategy is SINGLE_TABLE. But if you want to write the default strategy, it should be:

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)

Project.java

package com.hibernate.inheritancemapping;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Project {
	
	private int projectId;
	private String projectName;
	
	@Id
	@GeneratedValue
	public int getProjectId() {
		return projectId;
	}
	
	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}
	
	public String getProjectName() {
		return projectName;
	}
	
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

}

Module.java

package com.hibernate.inheritancemapping;

import javax.persistence.Entity;

@Entity
public class Module extends Project {
	
	private String moduleName;

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
	
}

Task.java

package com.hibernate.inheritancemapping;

import javax.persistence.Entity;

@Entity
public class Task extends Module {
	
	private String taskName;

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	
}

Main.java

package com.hibernate.inheritancemapping;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {
	
	public static void main(String[] args) {
		Configuration config = new Configuration();
		config.addAnnotatedClass(Project.class);
		config.addAnnotatedClass(Module.class);
		config.addAnnotatedClass(Task.class);
		config.configure();
		
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		Project p = new Project();
		p.setProjectName("Hibernate Lessons");
		
		Module m = new Module();
		m.setProjectName("Spring Lessons");
		m.setModuleName("AOP");
		
		Task t = new Task();
		t.setProjectName("Java Lessons");
		t.setModuleName("Collections");
		t.setTaskName("ArrayList");
		
		session.save(p);
		session.save(m);
		session.save(t);
		
		transaction.commit();
	}

}

After executing the Main class you will see the following table.

JOINED strategy
Run the application again after changing the strategy as JOINED at line 10 of the Project.java file like this.

@Inheritance(strategy=InheritanceType.JOINED)

After executing the Main class you will see the following tables.

TABLE_PER_CLASS strategy
Run the application again after changing the strategy as TABLE_PER_CLASS at line 10 of the Project.java file like this.

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

After executing the Main class you will see the following tables.

You can choose any strategy you want. There are advantages and disadvantages of each strategy.
SINGLE_TABLE: You will have all your data in one table but If you have too many columns in a particular class, you will have a large table.
JOINED: It does not repeat the data. My favorite strategy is JOINED.
TABLE_PER_CLASS: Notice that the PROJECTNAME or the MODULENAME gets repeated across various tables.So data has been repeated again and again in TABLE_PER_CLASS.

Advertisements

About kaanmutlu

Software Developer - Computer Engineer from Istanbul, Turkey
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s