Step 2: Entity Class and Persistence Unit

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

  • Open the [New Entity Class] dialog box, e.g. by right clicking the project node
    (in the [Projects] window) and selecting New > Entity Class... (or New > Other... > Persistence > Entity Class and clicking Next).
  • Enter Guest as the class name - use exactly that case sensitive class name.
  • Enter guest as the package name - use exactly that case sensitive package name.
  • Click Next to create the new entity class.

  • In the [Provider and Database] step just click Finish to generate a default persistence.xml file with a default persistence unit (that will be configured later). If the Finish button is disabled fill the fields with arbitrary values to enable it.

A new entity class that should represent Guest objects in the database was created in the project (under Source Packages > guest).

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

package guest;

import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Guest implements Serializable {
    private static final long serialVersionUID = 1L;

    // Persistent Fields:
    @Id @GeneratedValue
    Long id;
    private String name;
    private Date signingDate;

    // Constructors:
    public Guest() {

    public Guest(String name) { = name;
        this.signingDate = new Date(System.currentTimeMillis());

    // String Representation:
    public String toString() {
        return name + " (signed on " + signingDate + ")";

A default JPA persistence unit (with default settings) was generated in a persistence.xml file that was added to the project (under Configuration Files).

Open the persistence.xml file in a text editor (by right clicking and selecting Edit or by double click and then moving to the Source or XML tab in the editor window). Use copy and paste to replace the default content of the persistence.xml file with the following new content:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns=""

  <persistence-unit name="GuestbookPU" transaction-type="JTA">
      <property name="javax.persistence.jdbc.url" value="$objectdb/db/guests.odb"/>
      <property name="javax.persistence.jdbc.user" value="admin"/>
      <property name="javax.persistence.jdbc.password" value="admin"/>


Now ObjectDB should be used as a JPA provider with the specified database url.

The next step is adding an EJB Session Bean that will manage Guest entity objects.