我在从 hibernate 3.6 升级到 4.0.1(从 3 升级到 3.1)时遇到问题。
我在调用某些方法(例如 OnMessage 调用、Cron 更新程序等)时使用 hibernateinterceptors 来注入(inject) session ,并在 Web 请求中使用 OpennSessionInView 拦截器。它在 hib 3.6 和 spring 3.0 上运行良好,但是从 hibernate4 开始我无法让它工作。 hibernateInterceptor 仅在 hibernate3 包中可用,使用它不会使其工作
有什么想法我应该做什么?
删除拦截器使我能够开始工作,但是一旦我尝试从不是来自网络的请求中调用 dao,我就会得到“无 session 绑定(bind)异常”。
有没有更好的方法来拦截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)
或者很多这是一个错误,classic.Session 它存在于hibernate 4中吗?
异常是从试图访问 dao 的类的构造函数中引发的。这在 hibernate 3.6 中运行良好,但升级后我无法让它工作。
最佳答案
如果您在 spring 3.1 或 4.x 上使用 hibernate 4.x,您应该使用包中的类 org.springframework.orm.hibernate4
而不是 org.springframework.orm.hibernate3
HibernateInterceptor
之前已被弃用。您应该使用 org.springframework.orm.hibernate4.support.OpenSessionInterceptor
关于带有 spring 3.1 和 hibernate 4.01 的 HibernateInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871005/