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 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.io.Serializable;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
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) {
        this.name = name;
        this.signingDate = new Date(System.currentTimeMillis());
    }

    // String Representation:
    @Override
    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 Other Sources > src/main/resources > META-INF).

Open the persistence.xml file in a text editor (by right clicking and selecting Edit or by double click and then moving to the 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="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="GuestbookPU" transaction-type="RESOURCE_LOCAL">
    <provider>com.objectdb.jpa.Provider</provider>
    <class>guest.Guest</class>
    <properties>
      <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"/>
    </properties>
  </persistence-unit>

</persistence>

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

The next step is adding a Spring DAO Component class that will manage Guest entity objects.