HibernateInterceptor有Spring 3.1和hibernate 4.01

时间:2012-01-15 15:51:27

标签: hibernate spring

我遇到了从hibernate 3.6升级到4.0.1(从3到3.1)的问题。

我正在使用hibernateinterceptors在调用某些方法(例如OnMessage调用,Cron更新程序等)和Web请求的OpennSessionInView拦截器时注入会话。它已经与hib 3.6和spring 3.0一起工作正常,但是从hibernate4开始我无法使用它。 hibernateInterceptor仅在hibernate3包中可用,并且使用它不会使它工作

我应该做些什么?

删除拦截器的东西使我能够启动,但是一旦我尝试从不是来自Web的请求调用dao,我就会得到“No session bound exception”。

有没有更好的方法拦截dao然后使用hibernate拦截器或我应该使用另一种技术?如上所述,我正在使用来自web请求的dao(使用opensessioninview处理得很好),JMS OnMessage和SpringCron以及无法正常工作的初始化代码。

这是dao的基本设置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/jms 
    http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<bean id="someDao" class="org.springframework.aop.framework.ProxyFactoryBean"
    p:target-ref="someDaoTarget" p:proxyInterfaces="com.xxxx.MediaDataDao"
    p:interceptorNames="hibernateInterceptor" />

<bean id="someDaoTarget" class="com.xxxx.SomeDaoImpl"
    p:sessionFactory-ref="sessionFactory" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="hibernateInterceptor" 
  class="org.springframework.orm.hibernate3.HibernateInterceptor"
  p:sessionFactory-ref="sessionFactory" />

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
  destroy-method="destroy" 
  p:dataSource-ref="dataSource">
  <property name="packagesToScan"
value="com.xxxx" />
  <property name="hibernateProperties" ref="hibernateProperties" />
 </bean>

<util:properties id="hibernateProperties">
<prop key="hibernate.hbm2dll.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.jdbc.batch_size">500</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="javax.persistence.validation.mode">callback</prop> 
</util:properties>

使用hibernate拦截器时抛出的异常是:

Caused by: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at  org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:322)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233)
at org.springframework.orm.hibernate3.HibernateInterceptor.getSession(HibernateInterceptor.java:145)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy37.getAll(Unknown Source)

或许多这是一个bug,classic.Session是否存在于hibernate 4中?

从尝试访问dao的类的构造函数中抛出异常。这与hibernate 3.6完美配合,但升级后我无法使用它。

1 个答案:

答案 0 :(得分:3)

如果您在春季3.1或4.x上使用hibernate 4.x,则应使用包org.springframework.orm.hibernate4中的类而不是org.springframework.orm.hibernate3

HibernateInterceptor之前已被弃用。您应该使用org.springframework.orm.hibernate4.support.OpenSessionInterceptor