使用Hibernate / JPA在运行时抛出AbstractMethodError

时间:2015-07-14 14:53:26

标签: java hibernate jpa

我是Hibernate和JPA的新手,我在尝试按照JPA规范实现使用Hibernate的教程时遇到以下问题。

我有这些课程:

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)映射到数据库表的 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)最后,我的应用程序的 persistence.xml 文件夹中包含 META-INF 配置文件:

<?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>

当我尝试运行我的应用程序时,我在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)

2 个答案:

答案 0 :(得分:8)

我在尝试做hibernate教程时遇到了同样的问题。这通常意味着您添加到构建路径的jar文件之间存在兼容性问题。

在你的情况下,似乎是这样           的 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.getConfigurationValues() 在最新版本中作为抽象方法出现,因此出现上述错误。尝试将JPA jar更改为以前的版本,这可能会解决问题;这对我的情况有帮助。

答案 1 :(得分:1)

我遇到了同样的问题,当我检查了Maven存储库中的hibernate-entity管理器时,我发现我没有在我的POM.xml中指定最新版本。一旦我更新到最新版本,它修复了问题。