0

I am trying to configure Spring 4 with Hibernate 5.Made a google research but it didn't help. I am launching my app with JUnit test, just to see if Hibernate works.

Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [ConfigTest/datasource.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/boot/MetadataSources;)V; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [ConfigTest/datasource.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/boot/MetadataSources;)V

This is my datasource.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:component-scan base-package="/WebMVCtest/test/Config">
    </context:component-scan>

    <beans profile="dev">
        <context:property-placeholder
            location="ConfigTest/jdbc.properties" />

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">

            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="username" value="${jdbc.username}"></property>
        </bean>

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

                </props>
            </property>
            <property name="packagesToScan">
                <list>
                    <value>DAO</value>
                </list>
            </property>
        </bean>

    </beans>

</beans>

Also When I use Hibernate 4 I face an error :

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory

but I do have RegionFactory at that address: enter image description here

Thanful in advance!

  • Can you also show how the `usersDao` bean is defined? Is that also in XML? Or picked up in `context:component-scan`? – Drew Wills Nov 12 '16 at 18:16
  • It's in DAO package,that is picked up due to a `packagesToScan` property. UsersDao: `@Repository @Component("usersDao") public class UsersDao { private NamedParameterJdbcTemplate jdbc; @Autowired private SessionFactory sessionFactory; @Autowired private PasswordEncoder passwordEncoder; @Autowired public void setDataSource(DataSource jdbc) { this.jdbc = new NamedParameterJdbcTemplate(jdbc);} public Session session() { return sessionFactory.getCurrentSession();} @SuppressWarnings("unchecked") public List getAllUsers() { return session().createQuery("from User").list();}` – Yevhen Batsiun Nov 13 '16 at 17:06

2 Answers2

1

It appears that the packagesToScan property wants an array.

In your case, I suppose it would look like this...

<property name="packagesToScan">
    <array>
        <value>DAO</value>
    </array>
</property>

Or maybe simply...

<property name="packagesToScan" value="DAO" />
Community
  • 1
  • 1
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
  • Thank you for answer, but it didn't help. I ve got the same errors: using Hibernate 4 and class of SessionFactory `class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">` the error `Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory`And Hibernate 5, class of SessionFactory the same,but with hibernate5, error is the same as i gave higher. `OffersDAO` annotated the same as `UsersDao` . – Yevhen Batsiun Nov 13 '16 at 18:27
0

The problem was in compatability of versions. So I used Spring 4.0.3.RELEASE and Hibernate 4.3.5.Final and the error is gone.

Also I had an error : no current session found. So I've refractored a little a transaction bean :

<bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:annotation-driven />