ObjectDB ObjectDB

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 tutorial package node
    (in the [Projects] window) and selecting New > Java Class...
  • Enter Point as the class name - use exactly that case sensitive class name.
  • The package name should be tutorial.
  • 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 warning that NetBeans displays on the Guest class indicates that a persistence unit definition in an XML file is missing. This is discussed in the ObjectDB Manual. But nevertheless, this class is a valid ObjectDB entity class, despite the warning.

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