Tuesday, October 18, 2011

JPA + Hibernate with Spring

Spring provides support for JPA in two flavors. We can either use JpaTemplate or JpaDaoSupport. Let us look into both of them one by one.

Let us go to the package jpa.com.demo.JPAwithSpring and look into the files over there:

SpringForJPA.xml:

There are two types of entity managers. They are Application managed entity managers and Container managed entity managers. Each of these entity managers are obtained from their own entity manager factories. (i.e)
createEntityManagerFactory() of PersistenceProvider produces application managed entity manager.

createContainerEntityManagerFactory() of PersistenceProvider produces container managed entity manager.

Hence spring support both versions of the entity managers. In our example we used the container managed entity manager.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />

<property name="database" value="HSQL" />

<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
</property>
<property name="persistenceUnitName" value="jpaDemo" />
</bean>

* The entityManagerFactory represents an instance of LocalContainerEntityManagerFactoryBean * The attribute jpaVendorAdapter specifies the underlying ORM engine, here it is Hibernate.
* persistenceUnitName specifies the name of the persistence unit name in the persistence.xml

JpaTemplate:

<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>

* In the above xml configuration, the entityManagerFactory is wired into the JpaTemplate class which will produce entity managers.


<bean id="bookTemplateDao" class="jpa.com.demo.JPAwithSpring.BookJpaTemplateDaoImpl">
<property name="jpaTempate" ref="jpaTemplate" />
</bean>

* when the instance of the class BookJpaTemplateDaoImpl which has a reference to JpaTemplate is created from Spring, it is injected with an object of JpaTemplate as given above.

* The jpaTemplate then provides the methods like persist(), find() and getEntityManagerFactory() to deal with underlying database through the ORM engine. Look at the class BookJpaTemplateDaoImpl.java for more details.

Execute the class JpaTemplateExecutor.java from the package jpa.com.demo.JPAwithSpring and verify the results in the eclipse console as well as in the HSQL DB manager as follows.


JpaDaoSupport:

Instead of having a reference variable of type JpaTemplate, we can directly extend a class named JpaDaoSupport and directly use the method getJapTemplate() which will give the same methods that we have used from jpaTemplate.

<bean id="bookDaoSupportDao" class="jpa.com.demo.JPAwithSpring.BookJpaDaoSupportDaoImpl">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

* When the instance of a class BookJpaDaoSupportDaoImpl which extends the JpaDaoSupport is created from Spring, it is injected with an object of LocalContainerEntityManagerFactoryBean in the entityManagerFactory property as given above. For more details look at the class BookJpaDaoSupportDaoImpl.java.

Let us now execute the class JpaDaoSupportDaoExecutor.java and verify the results as explained above.