2016-11-12 6 views
0

私はHibernateでSpring 4を設定しようとしています。私は、Hibernateが動作するかどうかを確認するために、JUnitテストでアプリケーションを起動しています。名前 'usersDao'のBeanを作成するHibernate.ErrorでSpringを設定する: 'sessionFactory'フィールドで表現されている満足しない依存関係

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 

が、私はそのアドレスにRegionFactoryを持っています: enter image description here

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 

この

は私がエラーに直面休止状態4を使用する場合も、私の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> 

です

あらかじめご了承ください!

+0

'usersDao' beanがどのように定義されているかを示すことができますか?それはXMLでもですか?あるいは、 'context:component-scan'で拾いましたか? –

+0

DAOパッケージに入っています。これは 'packagesToScan'プロパティのために選択されます。ユーザーDao: '@Repository @Component(" usersDao ") public class UsersDao { private NamedParameterJdbcTemplate jdbc; @Autowired プライベートSessionFactory sessionFactory; @Autowired プライベートPasswordEncoder passwordエンコーダ; @Autowired公共ボイドsetDataSource(データソースJDBC){ this.jdbc =新しいNamedParameterJdbcTemplate(JDBC);};公共 @SuppressWarnings( "未チェック") 公開セッションのセッション(){ 戻りsessionFactory.getCurrentSession()}リスト getAllUsers(){ return session()。createQuery( "ユーザー")。リスト();} –

答えて

1

appears that the packagesToScan property wants an array。あなたのケースでは

、私はこの問題は、バージョンの互換性にあった

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

それとも単に...

<property name="packagesToScan" value="DAO" /> 
+0

ありがとうございました。私は同じエラーがあります:Hibernate 4とSessionFactoryのクラスを使用する 'class =" org.springframework.orm.hibernate4.LocalSessionFactoryBean ">'エラー '名前' offersDao 'を持つBeanを作成中にエラーが発生しました:autowired依存関係の注入が失敗しました。ネストされた例外はjava.lang.NoClassDefFoundErrorです:org/hibernate/cache/spi/RegionFactory'とHibernate 5、SessionFactoryのクラスは同じですが、hibernate5では、エラーは私が与えたものと同じです。 'OffersDAO'は' UsersDao'と同じアノテーションを付けました。 –

0

...それは次のようになりますと仮定します。だから私はSpring 4.0.3.RELEASEとHibernate 4.3.5.Finalを使い、エラーはなくなりました。

また、私はエラーがありました:現在のセッションが見つかりませんでした。だから私は小さなトランザクションBeanをrefractoredしました:

関連する問題