【问题标题】:Spring 4 + Spring repositories + hibernate 5: Error create java configSpring 4 + Spring 存储库 + hibernate 5:错误创建 java 配置
【发布时间】:2017-07-03 16:34:01
【问题描述】:

我有 spring 4 应用程序,我想使用 spring 存储库。我试过包括 spring jpa_hibernate

compile 'org.springframework.data:spring-data-jpa:1.11.4.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.0.5.Final'

并创建像oficial spring doc这样的配置:

@Configuration
@ComponentScan("my.domain")
@EnableJpaRepositories("my.domain")
@EnableTransactionManagement
public class ApplicationConfiguration {

    @Bean
    public Config getConfig() {
        return ConfigLoader.load();
    }

    @Bean
    @Autowired
    public DataSource getDatasource(Config config) throws Exception {
        Properties props = new Properties();
        Config dbConfig = config.getConfig("db.config");
        dbConfig.entrySet().forEach(entry -> props.put(entry.getKey(), entry.getValue().unwrapped()));
        return new DataSourceFactory().createDataSource(props);
    }

    @Bean
    @Autowired
    public NamedParameterJdbcTemplate getJdbcTemplate(DataSource datasource) {
        return new NamedParameterJdbcTemplate(datasource);
    }

    @Bean
    @Autowired
    public PlatformTransactionManager getTransactionManager(DataSource datasource) {
        return new DataSourceTransactionManager(datasource);
    }

    @Bean
    @Autowired
    public EntityManagerFactory entityManagerFactory(DataSource datasource) {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setDataSource(datasource);
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Bean
    @Autowired
    public PlatformTransactionManager transactionManager(DataSource datasource) {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory(datasource));
        return txManager;
    }
}

但尝试运行应用时出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in my.ApplicationConfiguration: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalStateException: 
Failed to determine Hibernate PersistenceProvider

我在 springboot 中使用了存储库并在文件中进行了配置,但我没有找到 java config spring 的实际示例(仅启动简单的核心应用程序)

【问题讨论】:

  • 请显示完整的堆栈跟踪
  • 这是完整的堆栈跟踪

标签: java spring hibernate repository spring-data-jpa


【解决方案1】:

当您使用最新的 Spring Data 版本时,请升级到最新的 Hibernate 版本:

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'

Spring 需要 hibernate 的 EntityManagerFactory 实现,该实现以前由单独的 jar 提供,现在已弃用,因此您只需要单个 hibernate-core 依赖项。

还可以考虑使用以下配置:

@Configuration
@ComponentScan("my.domain")
@EnableJpaRepositories("my.domain")
@EnableTransactionManagement
public class ApplicationConfiguration {

    @Bean
    public Config getConfig() {
        return ConfigLoader.load();
    }

    @Bean
    public DataSource getDatasource(Config config) throws Exception {
        Properties props = new Properties();
        Config dbConfig = config.getConfig("db.config");
        dbConfig.entrySet().forEach(entry -> props.put(entry.getKey(), entry.getValue().unwrapped()));
        return new DataSourceFactory().createDataSource(props);
    }

    @Bean
    public NamedParameterJdbcTemplate getJdbcTemplate(DataSource datasource) {
        return new NamedParameterJdbcTemplate(datasource);
    }

    @Bean
    @Autowired
    public PlatformTransactionManager getTransactionManager(DataSource datasource) {
        return new DataSourceTransactionManager(datasource);
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setDataSource(dataSource());
        factory.setPackagesToScan("my.domain");

        return factory;
    }

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

        return txManager;
    }

}

您参考的是旧的 spring 数据文档,当前版本是 available here

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2016-02-26
    • 2019-01-29
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多