SSCCE 在这里:https://github.com/dims12/TrySpringJpaPlusHibernate
我正在尝试在没有 persistence.xml 的情况下运行 Spring JPA 并具有以下配置:
@Configuration
@ComponentScan
@ImportResource("classpath:data_source.xml")
@EnableJpaRepositories("org.inthemoon.train.chinese.repositories")
public class BaseConfig {
@Autowired
private DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean ans =
new LocalContainerEntityManagerFactoryBean();
ans.setDataSource(dataSource);
ans.setJpaVendorAdapter(jpaVendorAdapter());
ans.setPackagesToScan("org.inthemoon.train.chinese.data");
return ans;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter ans = new HibernateJpaVendorAdapter();
ans.setShowSql(false);
ans.setGenerateDdl(true);
ans.setDatabase(Database.H2);
return ans;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager ans = new JpaTransactionManager();
ans.setEntityManagerFactory(emf);
return ans;
}
}
它导致以下异常
Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
...
附言有什么方法可以从第一次尝试配置 IoC
吗?
更新
我正在使用以下库:
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.5.Final'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.10.5.RELEASE'
更新 2
我尝试了 8 个不同版本的 hibernate-core
来构建 1.10.5.RELEASE
的 spring-jpa。
从 5.2.1
到 5.2.6
的版本导致相同的异常
NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
版本 5.1.3
和 5.0.11
导致
ClassNotFoundException: org.hibernate.ejb.HibernateEntityManagerFactory
唯一导致更复杂问题的版本是 5.2.0
。这是造成
SchemaManagementException: Attempt to resolve foreign key metadata from JDBC metadata failed to find column mappings for foreign key named [FKLOK22W31RKBMIIC2J96T9LTCN
产生的问题:
1) 这是否意味着版本 5.2.0
与 1.10.5
兼容?
2) 不做实验我怎么知道?
3) 以这种方式猜测版本是否正常?依赖管理工具的目的不就是为了避免这样的事情吗?如果 spring-data-jpa:1.10.5
依赖于 5.2.0
的 hibernate 那么为什么它没有在 POM
中描述?
更新 3
开箱即用示例:https://github.com/dims12/TrySpringJpaPlusHibernate
它不起作用。
最佳答案
Spring Data JPA v1.10.6 依赖于 Spring v4.2(准确地说是 v4.2.9),而 Spring v4.2 不支持 Hibernate v5.2。添加了对 Hibernate v5.2 的支持 only in Spring v4.3 .因此,您必须将Spring依赖升级到v4.3。
将以下依赖项添加到 Gradle 构建文件应该有效:
compile 'org.springframework:spring-beans:4.3.4.RELEASE'
compile 'org.springframework:spring-context:4.3.4.RELEASE'
compile 'org.springframework:spring-context-support:4.3.4.RELEASE'
compile 'org.springframework:spring-core:4.3.4.RELEASE'
compile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
compile 'org.springframework:spring-orm:4.3.4.RELEASE'
compile 'org.springframework:spring-tx:4.3.4.RELEASE'
您修改后的代码 available on Github .将 Gradle 测试作为 gradle test
运行以验证一切正常。
关于java - 如何找到 Hibernate + Spring-JPA 的兼容版本对? (无法打开 JPA EntityManager 进行交易),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550583/