gpt4 book ai didi

java - ObjectDB:未找到持久性异常类型错误 301

转载 作者:行者123 更新时间:2023-12-01 13:13:10 33 4
gpt4 key购买 nike

在学习本教程时:

http://www.objectdb.com/tutorial/jpa/eclipse

我收到以下错误:

Exception in thread "main" [ObjectDB 2.5.4_05] javax.persistence.PersistenceException
Type tutorial.model.Point is not found (error 301)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:437)
at objectdb.tutorial.main.Main.main(Main.java:25)
Caused by: com.objectdb.o.TEX: Type tutorial.model.Point is not found
at com.objectdb.o.MSG.e(MSG.java:107)
at com.objectdb.o.TYM.ay(TYM.java:1017)
at com.objectdb.o.TYM.ap(TYM.java:807)
at com.objectdb.o.TYM.ao(TYM.java:757)
at com.objectdb.o.TYM.at(TYM.java:873)
at com.objectdb.o.TYM.aw(TYM.java:945)
at com.objectdb.o.OBM.bB(OBM.java:371)
at com.objectdb.o.OBM.bB(OBM.java:257)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:434)
... 1 more
Caused by: java.lang.ClassNotFoundException: tutorial.model.Point
at com.objectdb.o.TYM.findClass(TYM.java:1033)
at com.objectdb.o.ACL.loadClass(ACL.java:131)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at com.objectdb.o.TYM.ay(TYM.java:1013)
... 8 more`

我查看了这篇文章( Problems searching empty user database with ObjectDB ),但由于某种原因,我的 EntityManager 实例没有成员函数 getMetamodel()。因此,我尝试创建自己的持久性单元,下面是我的代码。我仍然收到运行时错误,该错误似乎发生在第一个 for 循环中的 em.persist(p) 上。

package objectdb.tutorial.main;

import javax.persistence.*;

import objectdb.tutorial.model.Point;

import java.util.*;

public class Main {
public static void main(String[] args) {
// Open a database connection
// (create a new database if it doesn't exist yet):
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("my-pu");
EntityManager em = emf.createEntityManager();


// Store 1000 Point objects in the database:
em.getTransaction().begin();
for (int i = 0; i < 1000; i++) {
Point p = new Point(i, i);
em.persist(p);
}
em.getTransaction().commit();

// Find the number of Point objects in the database:
Query q1 = em.createQuery("SELECT COUNT(p) FROM Point p");
System.out.println("Total Points: " + q1.getSingleResult());

// Find the average X value:
Query q2 = em.createQuery("SELECT AVG(p.x) FROM Point p");
System.out.println("Average X: " + q2.getSingleResult());

// Retrieve all the Point objects from the database:
Query query =
em.createQuery("SELECT p FROM Point p");
List<Point> results = query.getResultList();
for (Point p : results) {
System.out.println(p);
}

// Close the database connection:
em.close();
emf.close();
}
}

实体:

package objectdb.tutorial.model;

import java.io.Serializable;
import javax.persistence.*;

@Entity
public class Point implements Serializable {
private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private long id;

private int x;
private int y;

public Point() {
}

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public Long getId() {
return id;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public String toString() {
return String.format("(%d, %d)", this.x, this.y);
}
}

持久性.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">

<persistence-unit name="my-pu">
<description>My Persistence Unit</description>
<provider>com.objectdb.jpa.Provider</provider>
<!-- <mapping-file>META-INF/mappingFile.xml</mapping-file> Not sure what this is-->
<class>objectdb.tutorial.model.Point</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="objectdb://localhost/tutorial.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>

</properties>
</persistence-unit>

</persistence>

我很感激任何建议。谢谢。

最佳答案

错误消息:“找不到类型tutorial.model.Point”指定包tutorial.model 中的Point 类。您的包名称是objectdb.tutorial.model。也许您以前有过该包名称,但您将包移动到了新的位置和名称。

尝试删除数据库文件,因为它仍然包含以前名称中的类(或者如果您需要数据,请按照 ObjectDB 网站上有关 schema change 的说明进行操作)。

如果您没有 getMetamodel 方法,则在 ObjectDB 之前的类路径中可能有一个旧的 JPA 1.0 jar(其中包括 JPA 2.1 jar)。您必须修复类路径以避免出现其他问题。

关于java - ObjectDB:未找到持久性异常类型错误 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676105/

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