在EntityManagerFactory中未指定PersistenceProvider,并且选择的PersistenceUnitInfo没有指定基于注释的配置

时间:2018-09-14 06:57:51

标签: java spring hibernate

我正在做基于注释的编码,我试图用Spring,Hibernate配置运行该应用程序,但由于错误而失败

Caused by: java.lang.IllegalArgumentException: No PersistenceProvider 
specified in EntityManagerFactory configuration, and chosen 
PersistenceUnitInfo does not specify a provider class name either

下面是我的代码

@SpringBootApplication
@EnableJpaRepositories
public class CurrExDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CurrExDemoApplication.class, args);
    }

    @Bean
    @ConfigurationProperties("app.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.currencyExchange.currExDemo");

        Properties props = new Properties();
        props.put("showSql", true);
        props.put("databasePlatform", Database.MYSQL);
        props.put("hibernate.hbm2ddl.auto", "create");
        em.setJpaProperties(props);

        return em;
    }
}

此代码有什么问题?

1 个答案:

答案 0 :(得分:1)

似乎您至少缺少JpaVendorAdapter

像这样添加它:

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabasePlatform(hibernateDialect);
emf.setJpaVendorAdapter(vendorAdapter);

hibernateDialect是例如org.hibernate.dialect.MySQL5Dialect,但这取决于您的数据库。