java - Spring 4 + Spring 存储库 + hibernate 5 : Error create java config

标签 java spring hibernate repository spring-data-jpa

我有 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配置spring的实际示例(不只启动简单的核心应用程序)

最佳答案

当您使用最新的 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 Data 文档,当前版本是 available here .

关于java - Spring 4 + Spring 存储库 + hibernate 5 : Error create java config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44890385/

相关文章:

Java 接口(interface)仅定义方法名称(即变量参数)

java - 使用JNA在JAVA中加载动态库是否有限制?

java - 使用 MapStruct 将 2 个字符串字段映射到 OffsetDateTime

spring - 服务层类应该是单例吗?

mysql - 如何进行删除查询?

java - Hibernate 自引用实体作为非空列

java - Hibernate 在 emddable websphere 容器中找不到 java :comp/websphere/ExtendedJTATransaction

java - 延迟初始化在 Spring MVC 3 中不起作用

java - 带条件的 Hibernate 调用函数

Hibernate更新主键,批量更新从update [0]返回意外的行数;实际行数 : 0; expected: 1