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

喜你入骨 提交于 2019-12-10 14:28:09

问题


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.


回答1:


  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).



来源:https://stackoverflow.com/questions/31925138/how-to-migrate-usage-of-jpatemplate-from-spring-3-2-to-4-1-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!