温馨提示:本文翻译自stackoverflow.com,查看原文请点击:spring - Could not initialize class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider

spring - 无法初始化类org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider

发布于 2020-03-31 23:21:16

我正在尝试学习Spring Data JPA,但遇到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in org.mql.gestionstages.config.PersistanceJPAConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider

我有以下配置类JPA

@Configuration
@EnableTransactionManagement
public class PersistanceJPAConfig {


   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em 
        = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "mypackage.models" });
      **JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();**
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());
      return em;
   }

   @Bean
   public DriverManagerDataSource dataSource(){
       DriverManagerDataSource dataSource = new DriverManagerDataSource();
       dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
       dataSource.setUrl("jdbc:mysql://localhost:3306/gestion_stages");
       dataSource.setUsername("root");
       dataSource.setPassword("");
       return dataSource;
   }

   @Bean
   public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
       JpaTransactionManager transactionManager = new JpaTransactionManager();
       transactionManager.setEntityManagerFactory(emf);

       return transactionManager;
   }

   @Bean
   public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
       return new PersistenceExceptionTranslationPostProcessor();
   }

   Properties additionalProperties() {
       Properties properties = new Properties();
       properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
       properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");

       return properties;
   }
}

这行抛出异常:

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

这些是我/WEB-INF/lib文件夹中所有的罐子

aspectjweaver-1.9.5.jar
hibernate-core-5.2.17.Final.jar
hibernate-entitymanager-5.2.17.Final.jar
hibernate-jpa-2.1-api-1.0.2.Final.jar
hibernate-jpamodelgen-5.2.17.Final.jar
javax.servlet.jsp.jstl-1.2.2.jar
javax.servlet.jsp.jstl-api-1.2.2.jar
mysql-connector-java-8.0.13.jar
org.eclipse.persistence.jpa-2.6.8.jar
spring-aop-5.2.2.RELEASE.jar
spring-aspects-5.2.2.RELEASE.jar
spring-beans-5.2.2.RELEASE.jar
spring-context-5.2.2.RELEASE.jar
spring-core-5.2.2.RELEASE.jar
spring-data-commons-2.2.2.RELEASE.jar
spring-data-jpa-2.2.2.RELEASE.jar
spring-expression-5.2.2.RELEASE.jar
spring-jcl-5.2.2.RELEASE.jar
spring-jdbc-5.2.2.RELEASE.jar
spring-orm-5.2.2.RELEASE.jar
spring-tx-5.2.2.RELEASE.jar
spring-web-5.2.2.RELEASE.jar
spring-webmvc-5.2.2.RELEASE.jar

查看更多

提问者
Amine
被浏览
18
Jens Schauder 2020-01-31 19:24

根本的例外是NoClassDefFoundError: Could not initialize class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider,这基本上意味着您缺少一个jar。

因此,要为应用程序收集所有必需的jar是PITA,我强烈建议切换到像Maven这样的依赖管理解决方案,在该解决方案中,您声明对Spring Data JPA的依赖,并为您收集所有临时依赖。