使用spring版本:4.3.1,Spring数据:1.10.2,hibernate core:5.2.1。 使用postgres Database pg连接器版本:9.4.1208.jre7 从hibernate 5.1.0升级到5.2.1,当我试图创建一个低于异常的实体时
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.cando.person.controller.UserController$$EnhancerBySpringCGLIB$$250a315a.createDefaultUser(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
... 31 more
Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.prepareFlushMode(HibernateJpaDialect.java:187)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:173)
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
... 51 more
java config
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( "com.cando.**.repository" )
@PropertySource( "classpath:spring.properties" )
public class SpringDataConfig
{
@Autowired
private Environment env;
// Declare a datasource
@Bean
public DataSource dataSource()
{
try
{
boolean productionProfile = checkProfileIsActive( IConstants.PROFILE_PRODUCTION );
String driverClassName = productionProfile ? env.getRequiredProperty( "production.app.jdbc.driverClassName" ) : env.getRequiredProperty( "dev.app.jdbc.driverClassName" );
String url = productionProfile ? env.getRequiredProperty( "production.app.jdbc.url" ) : env.getRequiredProperty( "dev.app.jdbc.url" );
String username = productionProfile ? env.getRequiredProperty( "production.app.jdbc.username" ) : env.getRequiredProperty( "dev.app.jdbc.username" );
String password = productionProfile ? env.getRequiredProperty( "production.app.jdbc.password" ) : env.getRequiredProperty( "dev.app.jdbc.password" );
System.out.println( driverClassName + " " + url + " " + username + " " + password );
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName( driverClassName );
dataSource.setUrl( url);
dataSource.setUsername( username );
dataSource.setPassword( password );
return dataSource;
}
catch ( Exception e )
{
throw new RuntimeException( e );
}
}
// Declare a JPA entityManagerFactory
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource( dataSource() );
entityManagerFactoryBean.setPackagesToScan( "com.cando.**.model" );
Properties properties = new Properties();
properties.put( "hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect" );
properties.put( "hibernate.show_sql", false );
properties.put( "hibernate.format_sql", false );
//properties.put( "hibernate.hbm2ddl.auto", "update" );
properties.put( "hibernate.hbm2ddl.auto", "create-drop" );
entityManagerFactoryBean.setJpaProperties( properties );
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl( Boolean.TRUE );
vendorAdapter.setShowSql( Boolean.TRUE );
entityManagerFactoryBean.setJpaVendorAdapter( vendorAdapter );
return entityManagerFactoryBean;
}
// Declare a transaction manager
@Bean
public JpaTransactionManager transactionManager()
{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory( entityManagerFactory().getObject() );
return transactionManager;
}
private boolean checkProfileIsActive( String profileName )
{
for ( String profile : env.getActiveProfiles() )
{
if ( StringUtils.equals( profileName, profile ) )
{
return true;
}
}
return false;
}
}
答案 0 :(得分:8)
这看起来像你选择的Spring Framework 4.3.0.RELEASE版本的Jar冲突。请将其降级为hibernate-core-5.1.0.Final.jar。
答案 1 :(得分:0)
这看起来很奇怪,因为在Spring Framework 4.3.0.RELEASE中添加了Hibernate 5.2 API的方法签名支持,它首先专门查找getHibernateFlushMode
,然后由getFlushMode
跟进。
我说你可能在类路径上有新的Hibernate库,但是你可能在类路径中有一个不兼容的旧版Spring Framework。
答案 2 :(得分:0)
我发现了实现该方法的类ConfigurationHelper
public static getFlushMode( Object value )
在包org.hibernate.jpa.internal.util中的hibernate-entitymanager-5.0.2.Final.jar中,与正确的方法签名冲突,该签名包括在hibernate-core-5.2.12.Final.jar中实现的默认值。
错误的实施得到了解决。我从我的gradle.build文件中删除了依赖hibernate-entitymanager-5.0.2.Final.jar,该文件已经包含了依赖hibernate-core-5.2.12.Final.jar并且整理了找不到异常的方法