510 words

Defining a JPA Entity Class

To be able to store Point objects in the database using JPA we need to define an entity class. A JPA entity class is a POJO (Plain Old Java Object) class, i.e. an ordinary Java class that is marked (annotated) as having the ability to represent objects in the database. Conceptually this is similar to serializable classes, which are marked as having the ability to be serialized.

The Point Entity Class

The following Point class, which represents points in the plane, is marked as an entity class, and accordingly, provides the ability to store Point objects in the database and retrieve Point objects from the database:

package com.objectdb.tutorial;
@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.
See JavaDoc Reference Page...
public class Point { // Persistent Fields: private int x; private int y;   // Constructor: Point (int x, int y) { this.x = x; this.y = y; }   // Accessor Methods: public int getX() { return this.x; } public int getY() { return this.y; }   // String Representation: @Override public String toString() { return String.format("(%d, %d)", this.x, this.y); } }

As you can see above, an entity class is an ordinary Java class. The only unique JPA addition is the @Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.
See JavaDoc Reference Page...
annotation, which marks the class as an entity class.

An attempt to persist Point objects in the database without marking the Point class as an entity class will cause a PersistenceExceptionjavax.persistence.PersistenceExceptionJPA exceptionThrown by the persistence provider when a problem occurs.
See JavaDoc Reference Page...
. This is similar to the NotSerializableException that Java throws (unrelated to JPA), on an attempt to serialize non serializable objects (except that all JPA exceptions are unchecked where Java IO exceptions are checked).

Persistent Fields in Entity Classes

Storing an entity object in the database does not store methods and code. Only the persistent state of the entity object, as reflected by its persistent fields is stored. By default, any field that is not declared as static or transient is a persistent field. In our example, the persistent fields of the Point entity class are x and y (representing the position of the point in the plane). It is the values of these fields that are stored in the database when an entity object is persisted.

Chapter 2 provides additional information on how to define entity classes, including which persistent types can be used for persistent fields, how to define and use a primary key and what a version field is and how to use it.

If you are already familiar with JPA you might have noticed that the Point entity class has no primary key (@Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.
See JavaDoc Reference Page...
) field defined. As an object database, ObjectDB supports implicit object IDs, so an explicitly defined primary key is not required. On the other hand, ObjectDB also supports explicit JPA primary keys,  including composite primary keys and automatic sequential value generation. This is a very powerful feature of ObjectDB that is absent from other object oriented databases.