Reputation: 5427
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?
Upvotes: 0
Views: 1072
Reputation: 1308
As answered above you need to set the classpath.
In the classpath you can just add objectdb.jar, which includes in addition to the ObjectDB Database implementation, all the required JPA types (e.g. EntityManagerFactory
).
So your command line in Windows could be:
> javac -classpath .;c:\objectdb\bin\objectdb.jar tutorial\*.java
when the current directory is the parent directory of the tutorial package directory.
Upvotes: 2