遵循本教程:
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);
}
}
persistence.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>
我感谢任何建议。谢谢。
答案 0 :(得分:1)
错误消息:&#34;未找到类型tutorial.model.Point&#34;指定包tutorial.model中的Point类。您的包名称是objectdb.tutorial.model。可能您以前拥有该软件包名称,但您已将软件包移至新位置和名称。
尝试删除数据库文件,因为它仍然包含以前名称的类(或者如果您需要数据,请按照ObjectDB网站上有关schema change的说明)。
如果您没有getMetamodel方法,那么在ObjectDB(包括JPA 2.1 jar)之前,您的类路径中可能有一个旧的JPA 1.0 jar。您必须修复类路径以避免其他问题。