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; @Entity @Table(name="MANAGER") @Inheritance public abstract class Manager { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; @Column 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; } @Override 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; @Entity public class ProjectManager extends Manager { private String currentProject; public String getCurrentProject() { return currentProject; } public void setCurrentProject(String currentProject) { this.currentProject = currentProject; } @Override 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(); em.getTransaction().begin(); ProjectManager manager = new ProjectManager(); manager.setName("Jack Thomson"); manager.setStartDate(new Date()); manager.setCurrentProject("IT Upgrade"); em.persist(manager); int managerId = manager.getId(); em.getTransaction().commit(); em.getTransaction().begin(); Manager dbManager = em.find(Manager.class, managerId); System.out.println("dbManager " + dbManager); em.getTransaction().commit(); em.close(); emf.close(); } }
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <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" /> </properties> </persistence-unit> </persistence>
Output:
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.