【问题标题】:JPA ObjectDB: compile and use itJPA ObjectDB:编译和使用它
【发布时间】: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 的可执行文件...... 我可以解决这个问题吗?

【问题讨论】:

    标签: java jpa objectdb


    【解决方案1】:

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

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

    所以你在 Windows 中的命令行可能是:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • ehm...应该怎么做呢?谢谢
      • sourceforge.net/projects/hibernate/files/hibernate4/4.2.1.Final/…下载hibernate orm库(hibernate是一个jpa提供者)然后将jar添加到你的eclipse中。
      • 我已将 jars 添加到 Eclipse,但问题是我无法从 shell (javac Main.java) 编译,因为它说 java.persistence 不存在......但我已经从日食
      • 如果你想从 shell 编译带有附加库的 java,你需要设置“classpath 参数”,它是包含附加库的路径java -classpath C:\hello\build\classes com.javahowto.test.HelloWorld
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多