简体   繁体   English

使用Hibernate / JPA在运行时引发AbstractMethodError

[英]AbstractMethodError thrown at runtime with Hibernate/JPA

I am absolutely new to Hibernate and JPA and I have the following problem trying to implement a tutorial that uses Hibernate following the JPA specification. 我对Hibernate和JPA绝对是陌生的,尝试实现遵循JPA规范使用Hibernate的教程时遇到以下问题。

I have these classes: 我有这些课:

1) A HelloWorldClient class, the entry point of my application, containing the main method: 1)一个HelloWorldClient类,它是我的应用程序的入口,包含main方法:

package client;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import entity.Message;

public class HelloWorldClient {
    public static void main(String[] args) {

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

        EntityManager em = emf.createEntityManager();
        EntityTransaction txn = em.getTransaction();
        txn.begin();

        Message message = new Message("Hello"); //transient state        
        em.persist(message); //persistent state        

        txn.commit();   
        em.close(); 

        message.setText("Hi"); //modifying the detached state of message

        EntityManager em2 = emf.createEntityManager();
        EntityTransaction txn2 = em2.getTransaction();
        txn2.begin();

        //the returned mergedMessage is a persistent object
        //any changes to mergedMessage will be dirty checked when the txn2 will be committed and updated in the database
        Message mergedMessage = em2.merge(message);

        txn2.commit();
        em2.close();        


        //Detaching objects explicitly
        /*
        EntityManager em3 = emf.createEntityManager();
        EntityTransaction txn3 = em.getTransaction();
        txn3.begin();

        Message msg = new Message("Howdy"); //transient state        
        em.persist(msg); //persistent state    

        em.detach(msg); //detaching the message object explicitly
        txn3.commit();
        em3.close();
        */  

    }
}

2) A Message entity class that is mapped on a database table: 2)映射到数据库表上的Message实体类:

package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="message")
public class Message {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ID")  
    private Long id;

    @Column(name="TEXT")    
    private String text;

    public Message() {}
    public Message(String text) {
        this.text = text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

3) And finally I have the persistence.xml configuration file in the META-INF folder of my application: 3)最后,我的应用程序的META-INF文件夹中有persistence.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="merging" transaction-type="RESOURCE_LOCAL">
        <properties>

            <!-- Database connection settings -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hello-world" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="myPassword" />

            <!-- SQL dialect -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

            <!-- Create/update tables automatically using mapping metadata -->
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <!-- Pretty print the SQL in the log file and console -->
            <property name="hibernate.format_sql" value="true" />
        </properties>

    </persistence-unit>
</persistence>

When I try to run my application, I obtain this error message in the stacktrace: 当我尝试运行我的应用程序时,我在stacktrace中获得以下错误消息:

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
Exception in thread "main" java.lang.AbstractMethodError: org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.getConfigurationValues()Ljava/util/Map;
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:404)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at client.HelloWorldClient.main(HelloWorldClient.java:14)

I've had the same issue when trying to do hibernate tutorials. 尝试进行休眠教程时,我遇到了同样的问题。 It usually means that there are compatibility issues between the jar files you added to your build path. 通常,这意味着添加到构建路径的jar文件之间存在兼容性问题。

in your case, it would seem that org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.getConfigurationValues() appears in the latest version as an abstract method, hence the above error. 在您的情况下,似乎org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.getConfigurationValues()作为最新方法出现在最新版本中,因此出现了上述错误。 Try changing the JPA jar to a previous version, that will probably solve the issue; 尝试将JPA jar更改为以前的版本,这可能会解决该问题; that's what helped in my case. 这对我来说很有帮助。

I had the same problem and when I checked the Maven repository for hibernate-entity manager I found I wasn't specifying the latest version in my POM.xml. 我遇到了同样的问题,当我检查Maven存储库中的休眠实体管理器时,发现我没有在POM.xml中指定最新版本。 Once I updated to the latest version it fixed the problem. 一旦我更新到最新版本,它便解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM