Step 2: Define a JPA Entity Class

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

  • Right click on the project in the [Package Explorer] window and select New > Class.
  • Enter tutorial as the package name (case sensitive).
  • Enter Point as the class name (case sensitive).
  • Click Finish to create the new class.

Copy and paste the following code into the newly created Point class:

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. Apart from the @Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. annotation and the id field (and its annotations) - the Point class is an ordinary Java class.

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