而下面這個教程:ObjectDB:持久性異常類型未找到錯誤301
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>
我明白任何建議。謝謝。
你完全正確。我的系統庫中有一箇舊版本的JPA。 –