2012-01-15 130 views
10

我遇到从休眠3.6升级到4.0.1(从3春到3.1)的问题。HibernateInterceptor与弹簧3.1和休眠4.01

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

任何想法我应该做什么?

删除拦截器的事情,使我能够开始的事情,但一旦我尝试从请求而不是从Web调用道的,我得到“没有会话绑定异常”。

有没有更好的方法来拦截道的然后使用休眠拦截器,或者我应该使用其他技术?如上所述,我使用来自web请求的dao(可以使用opensessioninview进行处理),JMS OnMessage和SpringCron,以及初始化代码不起作用。

下面是使用Hibernate的拦截器时抛出的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> 

异常的基本设置:

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它存在于休眠4中吗?

从尝试访问道的类的构造函数中抛出异常。这与hibernate 3.6完美配合,但是在升级后我无法得到这个工作。

回答

3

如果您使用的是弹簧3.1或4.x中你休眠4.x的,你应该使用类从包org.springframework.orm.hibernate4而不是org.springframework.orm.hibernate3

HibernateInterceptor前面已经过时。您应该使用org.springframework.orm.hibernate4.support.OpenSessionInterceptor