同时使用Hibernate和Spring数据jpa? [英] Simultaneous use of Hibernate and Spring data jpa?

查看:100
本文介绍了同时使用Hibernate和Spring数据jpa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用Spring Data JPA(由Hibernate支持作为JPA提供者)并直接同时使用Hibernate?



问题是,当我使用JpaTransactionManager,我无法用 org.hibernate.HibernateException检索当前会话:找不到当前线程的会话。当我切换到HibernateTransaction管理器时,JPA存储库不能提交更改。



这是我的Spring上下文的一部分(使用该上下文我无法使用直接Hibernate调用):

 < jee:jndi-lookup id =dataSourcejndi-name =jdbc / IPGCONF /> 

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean
p:dataSource-ref =dataSource>
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =entityManagerFactory/>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< property name =dataSourceref =dataSource/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>

< jpa:repositories base-package =com.satgate/>

hibernate存储库示例:

  public Collection< Layer> listCurrent(Carrier carrier){
Criteria query = sessionFactory.getCurrentSession()
.createCriteria(Layer.class)
.add(Restrictions.eq(carrier,carrier));
query.createCriteria(bitrate)
.addOrder(Order.desc(bitrate))
.add(Restrictions.eq(symbolrate,carrier.getSymbolrate())) ;
返回query.list();

Spring数据存储库定义示例:

  public interface BitrateRepository extends PagingAndSortingRepository< Bitrate,Long> {} 

软件版本:

 < org.springframework.version> 4.0.0.RELEASE< /org.springframework.version> 
< org.springframework.data.version> 1.4.3.RELEASE< /org.springframework.data.version>
< hibernate.version> 4.3.0.Final< /hibernate.version>

所以问题是 - 是否可以在同一个事务中使用(由@Transactional注解指定)Spring JPA存储库和直接Hibernate调用以及如何实现?解析方案

不用创建SessionFactory,而是使用 EntityManager.unwrap(Session.class)来获取一个Hibernate Session并从Session对象中检索会话工厂。



还可以使用 EntityManagerFactory.unwrap(SessionFactory.class)直接获取Hibernate SessionFactory。


Is it possible to use Spring Data JPA (backed by Hibernate as JPA provider) and directly use Hibernate at the same time?

The problem is that when i use JpaTransactionManager, i'm not able to retrieve current session with org.hibernate.HibernateException: No Session found for current thread. When i switch to HibernateTransaction manager, JPA repositories are not able to commit changes.

Here is the part of my Spring context (with that context i'm not able to use direct Hibernate calls):

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/IPGCONF"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

<jpa:repositories base-package="com.satgate"/>

Example of hibernate repository:

public Collection<Layer> listCurrent(Carrier carrier) {
    Criteria query = sessionFactory.getCurrentSession()
                    .createCriteria(Layer.class)
                    .add(Restrictions.eq("carrier", carrier));
    query.createCriteria("bitrate")
            .addOrder(Order.desc("bitrate"))
            .add(Restrictions.eq("symbolrate", carrier.getSymbolrate()));
    return query.list();
}

Example of Spring data repository definition:

public interface BitrateRepository extends PagingAndSortingRepository<Bitrate, Long> { }

Software versions:

<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.data.version>1.4.3.RELEASE</org.springframework.data.version>
<hibernate.version>4.3.0.Final</hibernate.version>

So, the question is - is it possible to use in the same transaction (specified by @Transactional annotation) both Spring JPA repositories and direct Hibernate calls and how to achieve that?

解决方案

Instead of creating a SessionFactory, use EntityManager.unwrap(Session.class) to get a Hibernate Session and retrieve the session factory from the Session object.

You can also use EntityManagerFactory.unwrap(SessionFactory.class) to get the Hibernate SessionFactory directly.

这篇关于同时使用Hibernate和Spring数据jpa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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