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?

zmirc
  • 795
  • 2
  • 10
  • 26
SagittariusA
  • 4,795
  • 15
  • 57
  • 112

2 Answers2

2

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.

ObjectDB
  • 1,294
  • 7
  • 8
1

You need to add the JPA libaries to the classpath.

Lukas Eichler
  • 3,983
  • 1
  • 19
  • 39
  • ehm...how should it do it? Thanks – SagittariusA May 07 '13 at 11:40
  • Download hibernate orm librarys(hibernate is an jpa provider) from http://sourceforge.net/projects/hibernate/files/hibernate4/4.2.1.Final/hibernate-release-4.2.1.Final.zip/download Then add the jars to your eclipse. – Lukas Eichler May 07 '13 at 11:53
  • I have added the jars to Eclipse but the problem is that I can't compile from shell (javac Main.java) because it says java.persistence does not exist...but I have added from eclipse – SagittariusA May 07 '13 at 11:55
  • If you want to compile java from shell with additional librarys you need to set the "classpath parameter" which is the path that contains the additional librarys `java -classpath C:\hello\build\classes com.javahowto.test.HelloWorld` – Lukas Eichler May 07 '13 at 11:58