Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate usage of JpaTemplate from Spring 3.2 to 4.1.4?

We currently have Spring 3.2.9.RELEASE configured and running (for a couples of years) and need to migrate to 4.1.4.RELEASE. We have an abstract DAO class that extends org.springframework.orm.jpa.support.JpaDaoSupport as well as other references to:

  • org.springframework.orm.jpa.JpaCallback
  • org.springframework.orm.jpa.JpaTemplate

I've seen that JpaDaoSupport has been removed in Spring 4. I've removed references to the Jpa* classes and replaced with

@PersistenceContext 
protected EntityManager theEntityManager;

and for method references in our DAO (like findByNamedParams()) found in JpaDaoSupport, and copied into our DAO.

After the above changes, we are able to compile our code but when it comes to running our JUnit tests, there is a reference in our applicationContext-test.xml of

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

<bean id="abstractDAO" abstract="true" class="my.company.package.AbstractDAO">
    <property name="jpaTemplate" ref="jpaTemplate" />
</bean>

<bean id="genericDAO" parent="abstractDAO" class="my.company.package.GenericDAO" />
<bean id="securityDAO" parent="abstractDAO" class="my.company.package.SecurityDAOImpl" />

Basically the error is there is no class reference of org.springframework.orm.jpa.JpaTemplate. How do we replace this JpaTemplate configuration for Spring 4.1.4?

Note that I'm picking this code up and wasn't the one who initially configured the system. Also I'm pretty new to Spring and its configuration setup.

like image 704
Jim Athrs Avatar asked Oct 30 '22 21:10

Jim Athrs


1 Answers

  1. Make sure AbstractDAO is migrated to the EntityManager API and uses @PersistenceContext on an injection point (a setter usually).
  2. Enable annotation configuration using <context:annotation-config /> or configure a PersistenceAnnotationBeanPostProcessor to enable the injection of the EntityManager into the DAOs. You can get rid of the bean definition for AbstractDAO entirely. Note, that if you have <context:component-scan /> activated somewhere, this should already work out of the box.

PS: You might wanna consider to upgrade to Spring 4.2 right away if you're migrating anyway. If that's not an option, please go with the latest Spring 4.1 version (4.1.7 at the time of writing).

like image 115
Oliver Drotbohm Avatar answered Nov 15 '22 11:11

Oliver Drotbohm