7.2. Introducing JPA

Java Persistence API (JPA) defines an easy and standard way to manage persistence in the Java EE and Java SE environments. The lightweight API defines a central coordinating persistence manager, called the EntityManager. Each persistence context, which essentially consists of a set of managed persistent entities that exist in a particular data store, has an EntityManager. The EntityManager API allows interaction with the persistence context and provides for the creation, persistence, and removal of entities.

Entities that are persistent have their defining classes annotated with the javax.persistence.Entity annotation. Classes can define their attributes as fields or properties. Sometimes both fields and properties can coexist in a single class. When a class adheres to the Java bean–style accessor pairs for its attributes, they are referred to as properties. In general, all but transient fields and properties are persisted to the data store.

7.2.1. Primary Key

Each persistent object is identified by a unique identifier, referred to as the primary key. If a single field or property is the primary key, then such a field or property is annotated with the javax.persistence.Id annotation. An example is:

@Entity
public class Person {
  ....

  String personId;

  @Id
  public String getPersonId{
    return personId;
  }
  ....

}

A String type property is the primary key in the preceding example. Other data types, namely the following, are also possible types for a primary key field ...

Get Professional BlazeDS: Creating Rich Internet Applications with Flex® and Java® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.