2016-07-04 34 views
2

我嘗試使用hibernate 5配置spring 4.3.1版本,但嘗試創建實體管理器工廠時出現錯誤。使用hibernate5配置spring4

我休眠版本5.0.9.Final」 Hibernate驗證版本 「5.2.4.Final」

我的依賴

  • 休眠核心5.0.9.Final
  • 休眠-的EntityManager 5.0.9.Final
  • 休眠-ehcache的5.0.9
  • 休眠驗證器5.2.4.Final
  • 休眠-JPA-2.1-API 1.0.0.Final
  • 驗證的API 1.0.0.GA

這是我的配置:

@Configuration 
@EnableTransactionManagement 
public class JpaConfiguration { 

@Value("classpath:hibernate.properties") 
private Properties jpaProperties; 

@Resource(name = "dataSource") 
private DataSource dataSource; 

/** 
* Enable exception translation for beans annotated with @Repository 
*/ 
@Bean 
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
    return new PersistenceExceptionTranslationPostProcessor(); 
} 

/** 
* @see read http://www.springframework.org/docs/reference/transaction.html 
*/ 
@Bean 
public JpaTransactionManager transactionManager() { 
    return new JpaTransactionManager(); 
} 

/** 
* Build the entity manager with Hibernate as a provider. 
*/ 
@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); 
    emf.setDataSource(dataSource); 
    // We set the persistenceXmlLocation to a different name to make it work on JBoss. 
    emf.setPersistenceXmlLocation("classpath:META-INF/spring-persistence.xml"); 
    emf.setPersistenceUnitName("myPU"); 
    emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 
    emf.setJpaProperties(jpaProperties); 
    emf.setJpaDialect(new HibernateJpaDialect()); 
    return emf; 
} 

@Bean 
public SessionFactory sessionFactory(HibernateEntityManagerFactory entityManagerFactory) { 
    return entityManagerFactory.getSessionFactory(); 
} 
} 

和我有錯誤:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.xxx.configuration.JpaConfiguration: Invocation of init method failed; nested exception is java.lang.AbstractMethodError 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at com.xxx.Boot$.main(Boot.scala:44) 
    at com.xxx.Boot.main(Boot.scala) 
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: java.lang.AbstractMethodError 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:278) 
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) 
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 18 more 

但是,當我將我的休眠版本從5更改爲4時,一切正常。

解析:

的問題是與org.jadira.usertype」 「usertype.core」 和我有不匹配與Hibernate版本4和5

+0

其中* exact * hibernate version ...在那之後添加您使用的依賴關係。 –

+2

當您在類路徑中混合使用Hibernate 4和Hibernate 5 jar時,看起來就像一個典型的錯誤。 – Gimby

+0

我的hibernate版本是5.0.9.Final和hibernate驗證器5.2.4.Final –

回答

1

的問題是與org.jadira .usertype「」usertype.core「,並且我在休眠版本4和5中有不匹配的類路徑

相關問題