HibernateTemplate可以和EntityManager共存吗? [英] Can HibernateTemplate coexist with EntityManager?

查看:857
本文介绍了HibernateTemplate可以和EntityManager共存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个spring 3应用程序,它仍然使用不推荐使用的 HibernateTemplate 作为持久性,并且希望迁移到更现代的JPA EntityManager

在迁移过程中是否可以同时使用两个API(甚至可能在单个事务中都使用这两个API),以便我们可以以小步骤进行迁移?



或者我们必须做大爆炸吗?

解决方案

当然,为什么不呢。



最简单的方法是放弃您的 LocalSessionFactoryBean HibernateTransactionManager 配置,并分别用 LocalContainerEntityManagerFactoryBean JpaTransactionManager 替换它。

然后获得 SessionFactory ,添加 HibernateJpaSessionFactoryBean ,公开 EntityManagerFactory 的底层 SessionFactory

通过这种方式,两种技术应该和平共存。



有些报告称,这样做会导致一个异常,指出没有配置CurrentSessionContext!。如果你得到它,将以下内容添加到 persistence.xml中

  < property name =hibernate.current_session_context_classvalue =org.springframework.orm.hibernate4.SpringSessionContext/> 

jpaProperties c> LocalContainerEntityManagerFactoryBean 。

 < property name =jpaProperties> 
<道具>
< prop name =hibernate.current_session_context_class> org.springframework.orm.hibernate4.SpringSessionContext< / prop>
<道具>
<属性>


We have a spring 3 application that still uses the deprecated HibernateTemplate for persistence and want to migrate to the more modern JPA EntityManager.

Is it possible to use both APIs in parallel during the migration (possibly even both in a single transaction), so that we can do the migration in small steps?

Or will we have to do it big bang?

解决方案

Sure, why not.

The easiest would be to drop your LocalSessionFactoryBean and HibernateTransactionManager configuration and replace it with LocalContainerEntityManagerFactoryBean and JpaTransactionManager, respectively.

Then to obtain a SessionFactory add the HibernateJpaSessionFactoryBean, which exposes the underlying SessionFactory for the EntityManagerFactory.

This way both technologies should peacefully coexist.

There are some reports that doing this leads to a an exception stating No CurrentSessionContext configured!. If you get it add the following to either your persistence.xml

<property name="hibernate.current_session_context_class" value="org.springframework.orm.hibernate4.SpringSessionContext"/>

or jpaProperties of the LocalContainerEntityManagerFactoryBean.

<property name="jpaProperties">
    <props>
        <prop name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    <props>
<property>

这篇关于HibernateTemplate可以和EntityManager共存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆