0

在遵循本教程时:

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`

我正在查看这篇文章(使用 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>

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

4

1 回答 1

1

错误消息:“Type tutorial.model.Point is not found”在包 tutorial.model 中指定了一个 Point 类。您的包名称是 objectdb.tutorial.model。可能您以前拥有该包名称,但您将包移动到了新的位置和名称。

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

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

于 2014-03-27T06:07:41.450 回答