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; ...
PS有没有办法IoC
从第一次尝试配置?
UPDATE
我正在使用以下libs:
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
spring-jpa 1.10.5.RELEASE
.
从版本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
依赖于hibernate 5.2.0
那么为什么不在其中描述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.仅在Spring v4.3中添加了对Hibernate v5.2的支持.因此,您必须将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'
您在Github上提供的修改后的代码.运行Gradle测试gradle test
以验证一切正常.