0

I'm trying to use JPA implementation called ObjectDB. I have downloaded jar files for ObjectDB and included them in my project with Eclipse. These are the two classes:

package tutorial;

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() {
 }

 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);
 }
}

and this is Main:

package tutorial;

import javax.persistence.*;
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("$objectdb/db/points.odb");
     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:
     TypedQuery<Point> query =
         em.createQuery("SELECT p FROM Point p", Point.class);
     List<Point> results = query.getResultList();
     for (Point p : results) {
         System.out.println(p);
     }

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

This little program only works from Eclipse by doing: Run As --> Java application

If I try to compile it, I get:

error: cannot find symbol
EntityManagerFactory emf =
symbol: class EntityManagerFactory
location: class Main

and so on for all others classes. This is strange because I have included External Jar to my project, the jar files containing the executable for ObjectDb... can I solve this?

4

2 回答 2

2

如上所述,您需要设置类路径。

在类路径中,您可以只添加 objectdb.jar,它除了 ObjectDB 数据库实现之外还包括所有必需的 JPA 类型(例如EntityManagerFactory)。

因此,您在 Windows 中的命令行可能是:

> javac -classpath .;c:\objectdb\bin\objectdb.jar tutorial\*.java

当前目录为教程包目录的父目录时。

于 2013-05-07T21:56:13.193 回答
1

您需要将 JPA 库添加到类路径中。

于 2013-05-07T11:35:51.737 回答