Wednesday, 24 July 2013

JPA Entity Manager

Setting up an EntityManager for JPA can be done in a number of ways.  Below is a simple example to get something going.  It uses spring 3 and the packagesToScan option so that a persistence.xml file is not required.

persistence.xml

The persistence.xml file normally lives in the META-INF directory of src/main/java but if the packagesToScan option is going to be used in the EntityManagerFactory then this file should not be present at all.

DataSource

The DataSource defines the database that is used.  For simple development this can just be included as shown below however, two useful changes can be done.  Firstly, externalise the properties to a property file that can be changed without a recompile.  Secondly the datasource can be obtained by looking up a JNDI resource from the application server.

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="awhite" />
<property name="password" value="passw0rd" />
</bean>

Whichever driver is used needs to be on the classpath of the application.  In this case oracle.jdbc.driver.OracleDriver.

EntityManagerFactory

Creating an EntityManagerFactory is shown below.  The 'databasePlatform' will change depending on the database that is being connected to.  The 'hibernate.show_sql' and 'format_sql' are useful for debugging purposes but could be configured in a property file to allow them to be changed later.

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.my.packages" />
        <property name="dataSource" ref="myDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
                <property name="generateDdl" value="false" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <!-- General settings -->
                <prop key="hibernate.archive.autodetection">class, hbm</prop>
                <prop key="hibernate.current_session_context_class">jta</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>
 

PersistenceAnnotationPostProcessor

The PersistenceAnnotationPostProcessor is included so that the EntityManager created by the JPA implementation (eg Hibernate) can still be wired into classes with spring even though it isn't spring that creates and manages that class.

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
 
This bean allows the @PersistenceContext annotation to be included in classes and the developer can assume that the EntityManager will be there at runtime. Here is an example of the annotation.

    /** JPA entity manager. */
    @PersistenceContext
    private EntityManager entityManager;










No comments:

Post a comment