2012-01-15 127 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