有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java未在EntityManagerFactory中指定PersistenceProvider,并且所选PersistenceUnitInfo未指定基于注释的配置

我正在进行基于注释的编码,我正在尝试使用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) 个答案

  1. # 1 楼答案

    看来你至少错过了JPA适配器

    添加如下内容:

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

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