我正在使用 Hibernate 5.3.1.Final 并尝试在我的 Maven 项目中连接到 Oracle。这些项目构建得很好,但是当我运行它时,出现以下错误。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in file 
 [C:\liferay-portal-6.1.1-ce-ga2\tomcat-7.0.27\temp\KIR\WEB-INF\classes\context\spring-context.xml]: 
 Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:  
 Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:  
 Constructor threw exception; nested exception is  
 java.lang.IncompatibleClassChangeError: class org.hibernate.ejb.HibernatePersistence has interface org.hibernate.ejb.AvailableSettings as super class 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323) 
        ... 41 more 

spring-context.xml 中的 Bean

<bean id="jpaVendorAdapter" 
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
        <property name="database" value="ORACLE" /> 
        <property name="showSql" value="false" /> 
        <property name="generateDdl" value="false" /> 
        <property name="databasePlatform" 
            value="org.hibernate.dialect.Oracle10gDialect" /> 
    </bean> 

下面是当前的 hibernate 和持久性 jar。

hibernate-commons-annotations-5.0.3.Final.jar 
hibernate-core-5.3.1.Final.jar 
hibernate-entitymanager-5.3.1.Final.jar 
hibernate-jpa-2.0-api-1.0.0.Final.jar 
hibernate-validator-5.0.1.Final.jar 
javax.persistence-api-2.2.jar 

有人可以告诉我这些 jar 之间是否有任何冲突以及是否首先需要所有这些 jar ?

Java 1.8

更新--

删除 hibernate4.1.10 特定 jar(核心、实体管理器工厂)会出现错误

 class not found :  org.hibernate.ejb.HibernatePersistence 

我在我的持久单元中使用这个类

持久单元

<persistence-unit name="kirPU" transaction-type="RESOURCE_LOCAL"> 
        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
        <mapping-file>\META-INF\orm.xml</mapping-file>  
</persistence-unit> 

Hibernate 5.3.1.FINAL jar 中是否缺少此类?

更新2

将提供商更改为 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\liferay-portal-6.1.1-ce-ga2\tomcat-7.0.27\temp\0-KIR\WEB-INF\classes\context\spring-context.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)V 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:439) 
        at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277) 
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139) 
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79) 
        at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:71) 
        at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:85) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1502) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1470) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) 
        ... 23 more 
Caused by: java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)V 

spring-context.xml 中的EntityMangerFactory bean

<bean id="entityManagerFactory" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="persistenceUnitName" value="kirPU" /> 
</bean> 

在 pom.xml 内

<dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-orm</artifactId> 
            <version>${spring.version}</version> 
            <exclusions> 
            <exclusion>   
              <groupId>org.hibernate</groupId> 
              <artifactId>hibernate-entitymanager</artifactId> 
            </exclusion> 
            <exclusion>   
              <groupId>org.hibernate</groupId> 
              <artifactId>hibernate-core</artifactId> 
            </exclusion> 
            <exclusion>   
              <groupId>org.hibernate</groupId> 
              <artifactId>hibernate-annotations</artifactId> 
            </exclusion> 
      </exclusions>  
        </dependency> 

Spring 版本:<spring.version>3.2.3.RELEASE</spring.version>

已排除,因为否则我会得到 hibernate jar 的重复版本。

请您参考如下方法:

org.hibernate.ejb.HibernatePersistence是libhibernate-entitymanager-3.5.0-Final.jar(或更早版本)的一部分,它必须驻留在某个地方在你的类(class)路径中。您必须将其删除。

在将项目升级为使用 Hibernat 5 后,您、您的 IDE 或应用程序服务器可能无法清理目标部署文件夹。

关于persistence.xml,您需要根据 documentation 迁移它。升级后的 hibernate 版本。它指出 org.hibernate.jpa.HibernatePersistenceProvider 是实际的 provider 类。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!