如何解决 Hibernate 5 中的 AbstractMethodError?

How to solve AbstractMethodError in Hibernate 5?

我正在尝试将 Java EE 中的休眠 5 与 tomcat 7 一起使用,但出现此错误:

java.lang.AbstractMethodError
org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:278)
org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
controller.dao.GenericDAO.<clinit>(GenericDAO.java:28)
com.mycompany.mazkrest.UsuarioResource.login(UsuarioResource.java:89)
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:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:309)
org.glassfish.jersey.internal.Errors.call(Errors.java:271)
org.glassfish.jersey.internal.Errors.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:460)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我使用的是 JPA 2.1,我删除了旧 jpa 版本的所有 jar。

这是我在 pom.xml 中的依赖项:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- <version>${jersey.version}</version>-->
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.voodoodyne.jackson.jsog</groupId>
        <artifactId>jackson-jsog</artifactId>
        <version>1.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <!--  <version>${jersey.version}</version>-->
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>5.3.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <type>jar</type>
    </dependency>
</dependencies>

当我第二次执行抛出异常的代码时,我得到一个 NoDefClassFoundError。

休眠似乎在日志中正确启动,但并未创建实体管理器。

如何解决?

当您用于编译的 jar 与服务器正在 运行 编译代码时引用的 jar 不兼容时,将抛出 AbstractMethodError。

http://docs.oracle.com/javase/7/docs/api/java/lang/AbstractMethodError.html

"当应用程序尝试调用抽象方法时抛出。通常,此错误会被编译器捕获;如果某些 class 的定义具有,则此错误只会在 运行 时发生自上次编译当前执行的方法以来发生了不兼容的更改。

这可能是由于 hibernate-search 和 hibernate-entity-manager 的版本问题

hibernate 搜索似乎正在使用 hibernate-commons-annotations 4.0.5 final

http://mvnrepository.com/artifact/org.hibernate/hibernate-search-engine/5.3.0.Final

鉴于

Hibernate 实体管理器似乎正在使用 hibernate-commons-annotation-5.0。0.Final

http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager/5.0.1.Final

在 Eclipse 中检查 pom.xml 的依赖层次结构选项卡,以找出正在使用的确切 jar 版本。用于排除不兼容的版本。通常这是由 pom 自动完成的。您还可以检查您的 WEB-INF/lib 文件夹以查看正在下载哪些 jar。

我还看到你正在使用这个罐子。如果您正在使用 tomcat 7 更好地使用它并使其范围为 "provided" 因为在 运行 时间网络应用程序引用 tomcat servlet api jar .

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>7.0.21</version>
    <scope>provided</scope>
    </dependency>