gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:42:26 24 4
gpt4 key购买 nike

我有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com