gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:39 26 4
gpt4 key购买 nike

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

我有这些类(class):

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) 最后我在 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>

当我尝试运行我的应用程序时,我在堆栈跟踪中收到此错误消息:

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)

最佳答案

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

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

关于java - 使用 Hibernate/JPA 在运行时抛出 AbstractMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com