【发布时间】:2013-05-07 11:28:02
【问题描述】:
我正在尝试使用名为 ObjectDB 的 JPA 实现。 我已经下载了 ObjectDB 的 jar 文件,并将它们包含在我的 Eclipse 项目中。这是两个类:
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);
}
}
这是主要的:
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();
}
}
这个小程序只能在 Eclipse 中通过以下方式工作: 运行方式 --> Java 应用程序
如果我尝试编译它,我会得到:
error: cannot find symbol
EntityManagerFactory emf =
symbol: class EntityManagerFactory
location: class Main
对于所有其他类,依此类推。 这很奇怪,因为我在项目中包含了 External Jar,这些 jar 文件包含 ObjectDb 的可执行文件...... 我可以解决这个问题吗?
【问题讨论】: