Hibernate 4升级(java.lang.NoSuchMethodError:javax.persistence.spi.PersistenceUnitInfo.getValidationMode())

时间:2013-12-09 23:21:30

标签: java spring hibernate maven upgrade

我正在尝试将我的应用程序从hibernate 3.4升级到4.1.0 FINAL版本并获得下面提到的错误。

我现在正在使用spring 3.2。我的pom文件如下所示。

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>com.springsource.org.hibernate</artifactId>
        <version>4.1.0.Final</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Hibernate JPA Provider -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>com.springsource.org.hibernate.ejb</artifactId>
        <version>4.1.0.Final</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>org.apache.commons</groupId>
                <artifactId>com.springsource.org.apache.commons.logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

以下是实体经理和会话工厂

    <bean id="testSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        autowire="byName">
        <property name="dataSource" ref="testDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>test</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
            </props>
        </property>
    </bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="unit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="dataSource">
            <ref bean="datasource" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.generate_statistics">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">"true"</prop>
            </props>
        </property>
    </bean>

以下是错误。

[ERROR] [main] 12-09 15:05:55,168 [ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [api-service.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at 

Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:633)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at 
[ERROR] [main] 12-09 15:05:55,169 [log] - Failed startup of context org.mortbay.jetty.webapp.WebAppContext@3a780024{/,file:/Users/Aanchal/avos/platform/src/avos-api/src/main/webapp/}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [api-service.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)

Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:633)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)

我按照以下链接升级了应用程序。感谢帮助。

3 个答案:

答案 0 :(得分:2)

您的运行时是Java EE 5(或更早版本),而代码期望Java EE 6.查看Java EE 5's PersistenceUnitInfo没有这样的方法getValidationModel()。您可以通过将运行时升级到Java EE 6来解决此问题

答案 1 :(得分:1)

我使用的是1.0.0版本的javax persistence lib

<dependency>
 <groupId>javax.persistence</groupId>
<artifactId>com.springsource.javax.persistence</artifactId>
<version>2.0.0</version>
 </dependency>

更改版本修复了它。,

答案 2 :(得分:0)

在我的情况下,我使用 spring-boot-jpa javax.persistence.jpa 导致依赖项崩溃。 删除javax.persistence.jpa 会有所帮助。

检查您的依赖项,检查某些依赖项是否重复。 (mvn依赖树) (在Idea中点击maven工具,然后点击显示相关性