2

My problem is same as this one - java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session

I get the following exception while using Spring JDBCTemplate.

java.lang.NoSuchMethodError: org.hibernate.Session.connection()Ljava/sql/Connection;

The accepted answer in the link above, recommends downgrading Hibernate to a version supported by Spring.

This is not acceptable to me as I need the latest version of Hibernate to use its new features (and also benefit from bug fixes).

Is there a way to get it to work without downgrading hibernate?

Community
  • 1
  • 1
Dojo
  • 5,374
  • 4
  • 49
  • 79
  • so which versions of Spring and Hibernate do you currently have? – S. Pauk Mar 28 '15 at 13:33
  • Spring is up to version 4.1.6; Hibernate's at 4.3.8. – duffymo Mar 28 '15 at 13:36
  • 2
    @duffymo you could go further: why Java? What is it doing for you that C or assembler cannot? – JB Nizet Mar 28 '15 at 13:37
  • Spring 3.0.5 and Hibernate is 4.3.8. I can upgrade Spring but there are several other projects that inter-operate and all of them are using Spring 3.0.5. 3.x to 4.x migration would break code in many places and necessitate code change. I don't have want to do that at this time...maybe later when we do a complete overhaul. Besides, why does Spring even have dependency on Hibernate? I was expecting Spring JDBC to be dependent only on JDBC. – Dojo Mar 28 '15 at 13:55
  • You can't expect a really really old version of Spring to be automatically compatible with the lastest non-compatible changes in Hibernate. Regarding the second part of your comment: what is the complete stack trace of the exception? spring-jdbc doesn't have any dependency on Hibernate, and I doubt the error you get comes from spring-jdbc code. – JB Nizet Mar 28 '15 at 14:08
  • @JBNizet - nope, that's a strawman. Spring JDBC is a perfectly good alternative to Hibernate. I'll bet that most people wouldn't be able to articulate why they chose Hibernate. Lots do it for reasons that don't make sense. I would say Hibernate and Spring JDBC template are orthogonal. I shouldn't expect to see them in the same use case. – duffymo Mar 28 '15 at 14:48

2 Answers2

1

I got this because I had upgraded from Hibernate4 to Hibernate 5 but still had a reference to 4 in the web.xml.

Was:

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>

changed to:

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>
0

The transaction manager being used was JPA transaction manager. This was a left over from the early days when we were using JPA via Spring. We no longer do that, so I was not expecting Spring to break on Hibernate version update. On searching the bean definition, I found the incorrect reference to JPA/Hibernate and removed it. The application runs fine now.

Changed this:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
    <property name="entityManagerFactory" ref="entityManagerFactory" />  
</bean>

To this:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
Dojo
  • 5,374
  • 4
  • 49
  • 79