307 words

Step 2: Define a JPA Entity Class

To store objects in an ObjectDB database using JPA we need to define an entity class:

  • Open the [New Java Class] dialog box, e.g. by right clicking the project node
    (in the [Package Explorer] window) and selecting New > Class.
  • Enter tutorial as the package name - use exactly that case sensitive package name.
  • Enter Point as the class name - use exactly that case sensitive class name.
  • Click Finish to create the new class.

Use copy and paste to fill the new source file with the following content:

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

The new class should represent Point objects in the database. Besides the @Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.
See JavaDoc Reference Page...
annotation and the id field (and its annotations) - the Point class is an ordinary Java class.

The next step is adding a Main class that stores and retrieves instances of the Point entity class.