2016-03-26 14 views
1

私はSpring MVC 4.2.5.RELEASEとHibernate 5.1.0.Finalを使用しています。私はすべてのbeanとpom.xmlを設定しました。私は、アプリケーションを実行して取得:SessionFactory NoClassDefFoundError in Hibernate 5 and Spring 4 MVC

SEVERE: Context initialization failed 
java.lang.NoClassDefFoundError: org/hibernate/SessionFactory 

私のpom.xml:

<properties> 
    <spring.version>4.2.5.RELEASE</spring.version> 
    <hibernate.version>5.1.0.Final</hibernate.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-dao</artifactId> 
     <version>2.0.8</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.38</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0.2</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>${hibernate.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>${hibernate.version}</version> 
     <exclusions> 
      <exclusion> 
       <groupId>cglib</groupId> 
       <artifactId>cglib</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>dom4j</groupId> 
       <artifactId>dom4j</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-ehcache</artifactId> 
     <version>${hibernate.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate.javax.persistence</groupId> 
     <artifactId>hibernate-jpa-2.0-api</artifactId> 
     <version>1.0.1.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib-nodep</artifactId> 
     <version>2.2</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.transaction</groupId> 
     <artifactId>jta</artifactId> 
     <version>1.1</version> 
    </dependency> 
</dependencies> 

HibernateConfig.java:

import mojaapka.dao.UserDAO; 
import mojaapka.dao.UserDAOImpl; 
import mojaapka.model.User; 
import org.apache.commons.dbcp.BasicDataSource; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; 
import org.springframework.orm.hibernate5.HibernateTransactionManager; 

import javax.sql.DataSource; 
import java.util.Properties; 

@Configuration 
public class HibernateConfig { 

@Bean 
public DataSource dataSource() { 
    BasicDataSource dataSource = new BasicDataSource(); 
    dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
    dataSource.setUrl("jdbc:mysql://localhost:3306/bonafide"); 
    dataSource.setUsername("root"); 
    dataSource.setPassword("pass"); 
    return dataSource; 
} 

@Bean 
@Autowired 
public SessionFactory sessionFactory(DataSource dataSource) { 
    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource); 
    sessionBuilder.addAnnotatedClasses(User.class); 
    return sessionBuilder.buildSessionFactory(); 
} 

private Properties getHibernateProperties() { 
    Properties properties = new Properties(); 
    properties.put("hibernate.show_sql", "true"); 
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
    return properties; 
} 

@Bean 
@Autowired 
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) { 
    return new HibernateTransactionManager(sessionFactory); 
} 

@Bean 
@Autowired 
public UserDAO userDAO(SessionFactory sessionFactory) { 
    return new UserDAOImpl(sessionFactory); 
} 
} 

は、私が行方不明ですか豆が設定されているいくつかの依存性があります間違っている?

+0

あなたのIDEとしてIntelliJ Ideaを使用していますか? –

+0

はい、私はIntellijを使用しています –

答えて

2

以下configurationaにあなたのSessionFactory豆を変更し、それが何の違いになりかどうかを確認:

@Configuration 
@EnableTransactionManagement 
@ComponentScan({ "com.xxxxxx.configuration.hib" }) 

public class HibernateConfiguration { 


    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
     sessionFactory.setDataSource(dataSource()); 
     sessionFactory.setPackagesToScan(new String[] { "com.XXXXXX.spring.component" }); 
     sessionFactory.setHibernateProperties(hibProperties()); 
     return sessionFactory; 
    } 

    @Bean 
    public DataSource dataSource() { 
     return driverManagerDataSource; 
    } 
    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(Environment.DIALECT, "org.hibernate.dialect.SQLServer2012Dialect"); 
     properties.put(Environment.SHOW_SQL, false); 
     properties.put(Environment.USE_SECOND_LEVEL_CACHE,true); 
     properties.put(Environment.CACHE_REGION_FACTORY,"org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"); 
     properties.put(Environment.CACHE_PROVIDER_CONFIG,"/ehcache.xml"); 
     properties.put(Environment.USE_QUERY_CACHE, true); 
     return properties; 
    } 

    @Bean 
    @Autowired 
    public HibernateTransactionManager transactionManager(SessionFactory s) { 
     HibernateTransactionManager txManager = new HibernateTransactionManager(); 
     txManager.setSessionFactory(s); 
     return txManager; 
    } 
} 

************輸入を********

import javax.sql.DataSource; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Environment; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
+0

いくつかの種類の進歩があります。次のようなエラーが発生しました:重大度:コンテキストの初期化に失敗しました java.lang.NoClassDefFoundError:org/springframework/orm/hibernate4/LocalSessionFactoryBean –

+0

autowiredアノテーションを持っています。削除してからもう一度やり直してください。 – LearningPhase

+0

いいえ、私はもうアノテーションを持っていません。 –

関連する問題