title: HibernateInterceptor with spring 3.1 and hibernate 4.01
body: <p>I'm running into problems upgrading from hibernate 3.6 to 4.0.1 (spring from 3 to 3.1).</p>
  
  <p>I'm using hibernateinterceptors for injecting the session when calling some methods (e.g. a OnMessage call, Cron updater etc) and OpennSessionInView interceptor for the web requests. It has been working fine with hib 3.6 and  spring 3.0, but as of hibernate4 I can't get it to work. The hibernateInterceptor is only available in the hibernate3 package and using that will not make it work</p>
  
  <p>Any ideas what I should do ?</p>
  
  <p>Removing the interceptor thing make me able to start things, but once I try calling the dao's from a request not from the web, I'm getting "No session bound exception". </p>
  
  <p>Is there a better way to intercept the dao's then using the hibernate interceptor or should I use another technique? As said, I'm using the dao's from web requests (which are handled fine with the opensessioninview), JMS OnMessage, and SpringCron and in initialization code which is not working.</p>
  
  <p>Here's the basic setup for the dao's  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  &lt;bean id="someDao" class="org.springframework.aop.framework.ProxyFactoryBean"
      p:target-ref="someDaoTarget" p:proxyInterfaces="com.xxxx.MediaDataDao"
      p:interceptorNames="hibernateInterceptor" /&gt;
  
  &lt;bean id="someDaoTarget" class="com.xxxx.SomeDaoImpl"
      p:sessionFactory-ref="sessionFactory" /&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
  &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateInterceptor" 
    class="org.springframework.orm.hibernate3.HibernateInterceptor"
    p:sessionFactory-ref="sessionFactory" /&gt;
  
  &lt;bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    destroy-method="destroy" 
    p:dataSource-ref="dataSource"&gt;
    &lt;property name="packagesToScan"
  value="com.xxxx" /&gt;
    &lt;property name="hibernateProperties" ref="hibernateProperties" /&gt;
   &lt;/bean&gt;
  
  &lt;util:properties id="hibernateProperties"&gt;
  &lt;prop key="hibernate.hbm2dll.auto"&gt;update&lt;/prop&gt;
  &lt;prop key="hibernate.connection.autocommit"&gt;false&lt;/prop&gt;
  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
  &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
  &lt;prop key="hibernate.jdbc.batch_size"&gt;500&lt;/prop&gt;
  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
  &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;
  &lt;prop key="javax.persistence.validation.mode"&gt;callback&lt;/prop&gt; 
  &lt;/util:properties&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p></p>
  
  <p>Exception thrown when using the hibernate interceptor is: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Or many this is a bug, the classic.Session does it exist in hibernate 4 ?</p>
  
  <p>The exception is thrown from within a constructor of a class which tries to access a dao. This was working perfectly well with hibernate 3.6 but after upgrade I can't get this to work.</p>