Warm tip: This article is reproduced from stackoverflow.com, please click
exception spring spring-data-jpa

Could not initialize class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider

发布于 2020-03-31 22:55:45

I'm trying to learn Spring Data JPA and I'm getting the following exception:

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

I have the following configuration class for 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;
   }
}

The exception is thrown in this line:

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

These are all the jars I have in my /WEB-INF/lib folder:

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
Questioner
Amine
Viewed
48
Jens Schauder 2020-01-31 19:24

The root exception is NoClassDefFoundError: Could not initialize class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider which basically means you are missing a jar.

To gather all the required jars for an application is a PITA, I therefore strongly suggest to switch to a dependency management solution like Maven where you declare a dependency to Spring Data JPA and it gathers all the transient dependencies for you.