JPA Joined Inheritance

David Winterfeldt

2009


This shows a JPA configuration for inheritance. There are three different types, which are single table, joined, and table per class. This example uses joined inheritance. Joined inheritance involves a base table for shared fields and a table for each subclass. There is a Person class that is the parent for Student and Professional. This example is based on One to Many JPA Hibernate Configuration, but has been modified enough to add inheritance.

1. Spring Configuration

The Person DAO is configured using context:component-scan. Then tx:annotation-driven will configure transactions on any beans with @Transactional, and just after it the JPA transaction manager is setup. The HsqldbInitializingDriverManagerDataSource is used to initialize the test HSQLDB database. The LocalContainerEntityManagerFactoryBean is configured with the DataSource and for use with Hibernate as the JPA adapter. The persistenceUnitName attribute is set to specify which persistence unit to use (multiple examples use the persistence.xml).

PersonInheritanceDaoTest-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="org.springbyexample.orm.jpa.inheritance.dao" />
    
    <tx:annotation-driven />

    <bean id="transactionManager" 
          class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>
    
    <bean id="dataSource"
          class="org.springbyexample.jdbc.datasource.HsqldbInitializingDriverManagerDataSource">
        <property name="sqlScriptProcessor">
            <bean class="org.springbyexample.jdbc.core.SqlScriptProcessor"
                  p:charset="UTF-8"
                  p:sqlScripts="classpath:/schema_inheritance.sql" />
        </property>
    </bean>
		
    <bean id="entityManagerFactory" 
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    	  p:dataSource-ref="dataSource"
          p:persistenceUnitName="inheritance-jpa">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>
    
</beans>