Home » Enterprise Java » jpa » Single table inheritance in JPA

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Single table inheritance in JPA

In this example we shall show you how to create a single table inheritance in JPA. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.

Here we shall show you how to use inheritance in JPA.

Manager and ProjectManager classes

The Manager class is an abstract class, that uses the @Inheritance annotation that defines the inheritance strategy to be used for an entity class hierarchy. It uses the @Table annotation to define the name of the table.

package com.javacodegeeks.snippets.enterprise;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Table;

public abstract class Manager  {
    private int id;
    private String name;
    private Date startDate;
    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 Date getStartDate() {

  return startDate;

    public void setStartDate(Date startDate) {
  this.startDate = startDate;

	public String toString() {
		return "Manager [id=" + id + ", name=" + name + ", startDate="
				+ startDate + "]";

The ProjectManager class is the annotated @Entity class that extends the Manager class and inherits its fields.

package com.javacodegeeks.snippets.enterprise;

import javax.persistence.Entity;

public class ProjectManager extends Manager {
	private String currentProject;

	public String getCurrentProject() {
		return currentProject;

	public void setCurrentProject(String currentProject) {
		this.currentProject = currentProject;

	public String toString() {
		return "ProjectManager [currentProject=" + currentProject
				+ ", toString()=" + super.toString() + "]";

The SingleTableInheritanceInJPA class

In SingleTableInheritanceInJPA class we create an EntityManagerFactory interface to interact with the entity manager factory for MyPeristenceUnit, that is defined in persistence.xml file. We create an EntityManager, using the createEntityManager() API method. Then, we create a new ProjectManager object. The new object is writen to the database, using the persist(java.lang.Object entity) API method of EntityManager. The getTransaction().begin() and getTransaction().commit() methods are used before and after the EntityManager invokes a method so that a transaction begins and ends. The Manager can be retrieved using the find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) API method of EntityManager.

package com.javacodegeeks.snippets.enterprise;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class SingleTableInheritanceInJPA {
	public static void main(String[] args) {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistenceUnit");
		EntityManager em = emf.createEntityManager();
		ProjectManager manager = new ProjectManager();
		manager.setName("Jack Thomson");
		manager.setStartDate(new Date());
		manager.setCurrentProject("IT Upgrade");
		int managerId = manager.getId();
		Manager dbManager = em.find(Manager.class, managerId);
		System.out.println("dbManager " + dbManager);




<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.username" value="jcg" />
			<property name="hibernate.connection.password" value="jcg" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost/companydb" />


dbManager ProjectManager [currentProject=IT Upgrade, toString()=Manager [id=1, name=Jack Thomson, startDate=Thu Dec 01 20:06:42 EET 2011]]

This was an example of how to create a single table inheritance in JPA.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments