2016-07-16 188 views
1

我使用Jersey作爲休息Web服務和Hibernate實體管理器來堅持JPA模型。我使用Tomcat 8作爲容器。休眠錯誤java.lang.NoSuchMethodError

下面是persistence.xml文件的內容:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
      version="2.0"> 

    <persistence-unit name="manager1" transaction-type="JTA"> 

     <properties> 

      <!--PostgreSQL--> 
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> 
      <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
      <property name="hibernate.connection.username" value="postgres"/> 
      <property name="hibernate.connection.password" value="postgres"/> 
      <property name="hibernate.archive.autodetection" value="class"/> 
      <property name="hibernate.id.new_generator_mappings" value="true"/> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
      <property name="hibernate.ejb.metamodel.generation" value="disabled"/> 
      <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/> 
      <property name="hibernate.show_sql" value="true"/> 
     </properties> 

    </persistence-unit> 

</persistence> 

我的REST調用是如此簡單:

@GET 
@Path("/") 
public Response test() { 

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1"); 

    return Response.status(Response.Status.OK).build(); 
} 

,這裏是我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <display-name>Restful Web Application</display-name> 

    <servlet> 
    <servlet-name>jersey-serlvet</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>ca.products.services</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 

     <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>jersey-serlvet</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

但它給了我這個錯誤:

type Rapport d''exception 

message java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V 

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête. 

exception 

javax.servlet.ServletException: java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V 
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420) 
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
cause mère 

java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V 
    org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.generateDefaultReflectionManager(MetadataBuilderImpl.java:736) 
    org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:709) 
    org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127) 
    org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135) 
    org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:185) 
    org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:34) 
    org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:165) 
    org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:114) 
    org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71) 
    org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52) 
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48) 
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) 
    ca.products.services.RestServices.test(RestServices.java:42) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    java.lang.reflect.Method.invoke(Method.java:497) 
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) 
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) 
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) 
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) 
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

我無法弄清楚問題在哪裏!任何幫助?

的建議,這裏是MVN依賴關係:樹日誌:

[INFO] ca.products.jpa_module:jpa_module:jar:1.0-SNAPSHOT 
[INFO] +- org.hibernate:hibernate-core:jar:5.0.7.Final:compile 
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile 
[INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile 
[INFO] | +- org.javassist:javassist:jar:3.18.1-GA:compile 
[INFO] | +- antlr:antlr:jar:2.7.7:compile 
[INFO] | +- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile 
[INFO] | +- org.jboss:jandex:jar:2.0.0.Final:compile 
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile 
[INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile 
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile 
[INFO] +- org.hibernate:hibernate-annotations:jar:3.5.6-Final:compile 
[INFO] | +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile 
[INFO] | \- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile 
[INFO] +- org.hibernate:hibernate-search-orm:jar:5.5.2.Final:compile 
[INFO] | \- org.hibernate:hibernate-search-engine:jar:5.5.2.Final:compile 
[INFO] |  +- org.apache.lucene:lucene-core:jar:5.3.1:compile 
[INFO] |  +- org.apache.lucene:lucene-backward-codecs:jar:5.3.1:compile 
[INFO] |  +- org.apache.lucene:lucene-misc:jar:5.3.1:compile 
[INFO] |  +- org.apache.lucene:lucene-analyzers-common:jar:5.3.1:compile 
[INFO] |  \- org.apache.lucene:lucene-facet:jar:5.3.1:compile 
[INFO] |  \- org.apache.lucene:lucene-queries:jar:5.3.1:compile 
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.0.7.Final:compile 
[INFO] +- org.hibernate:hibernate-ehcache:jar:5.0.7.Final:compile 
[INFO] | \- net.sf.ehcache:ehcache-core:jar:2.4.3:compile 
[INFO] +- com.google.inject.extensions:guice-assistedinject:jar:4.0:compile 
[INFO] | \- com.google.inject:guice:jar:4.0:compile 
[INFO] |  +- javax.inject:javax.inject:jar:1:compile 
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile 
[INFO] |  \- com.google.guava:guava:jar:16.0.1:compile 
[INFO] +- com.google.inject.extensions:guice-persist:jar:4.0:compile 
[INFO] +- log4j:log4j:jar:1.2.17:compile 
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.12:compile 
[INFO] +- org.slf4j:slf4j-api:jar:1.7.12:compile 
[INFO] \- commons-logging:commons-logging:jar:1.1.1:compile 
+1

您是否在使用任何構建工具,例如'Maven'或'Gradle'?通常,這種錯誤是由於庫不匹配造成的。請查看jar文件依賴關係樹。這兩個工具都能夠爲被調用的jar列表提供依賴關係樹。 –

+0

我正在使用Maven,並且我運行了mvn依賴關係:樹和沒有任何我可以看到的不正確!我會在帖子中粘貼mvn樹謝謝! – taboubim

+0

您有權@RavindranKanniah :)有問題與依賴關係:糾正我使用的唯一休眠核心代替的事情休眠核心冬眠的註解 taboubim

回答

6

基於@Ravindran Kanniah這裏的評論是答案:

有依賴關係休眠核和衝突

: 休眠的註解

所以我通過註釋神器給衝突糾正了我的Maven的pom.xml