Show TOC Start of Content Area

Background documentation Java Persistent Entities  Locate the document in its SAP Library structure

Lightweight Persistence

Java EE 5 provides a unified persistence model, the Java Persistence API. The persistence model is not limited to EJBs only. Persistent classes can be used directly in other components, for example, Web applications or components outside the Java EE application server, such as application clients. In the Java Persistence API, entities are Plain Old Java Objects (POJOs) that benefit from a standardized object relational mapping using annotations. Entities support inheritance, polymorphism, and optimistic locking. As entities are POJOs, they can be packaged anywhere in a Java enterprise application.

Entity Manager API

Java EE 5 Persistence provides the Entity Manager API, which is the main API for application developers to interact with the database. The EM manages the life cycle and state of entities. It supports create, read, update, and delete (CRUD) operations, query execution, and so on. The Java Persistence API also supports a detach and merge mechanism. Entities are detached from the EM at the end of the persistence context or when they get serialized. Detached entities can be manipulated in persistence-unaware environments like plain Java objects. They can also be merged into a different persistence context by the Entity Manager.

More information:

Obtaining an Entity Manager Instance

Managing Entity Instances

Java Persistence Query Language

The former EJB 2.1 Query Language has been enhanced to support named queries, queries with named parameters, dynamic queries, and bulk updates and deletes. The Java Persistence API also supports native SQL queries.

More information: Creating and Executing Queries

Requirements on Entity Classes

The Java Persistence API 1.0 defines a new programming model for mapping objects from an enterprise application to an underlying database. A persistence class, that is, an entity, is a Plain Old Java Object (POJO) annotated to map a persistence schema. The JPA specification requires that entities:

      Are annotated with the @Entity annotation.

      Have a public or protected no-argument constructor.

      Are not final: no method or variable can be final.

      Have private, package-private, or protected instance variables that keep the persistent state and may only be accessed by clients of the entity through its getter and setter methods.

      Expose their persistent state to the Entity Manager (EM) through either the instance variables (field-based access) or through public or protected getter/setter methods (property-based access). Depending on the type of access you choose, you annotate either fields or methods.

      Have one or more fields that form their unique identifier, that is, their primary key.

As JPA employs configuration by exception, the only mandatory annotations on an entity class are @Entity (to denote the class an entity class) and @Id (to denote the unique identifier field). Many common tasks have standardized O/R mapping. For example, an entity class usually maps to a table in the database and fields map to columns in the database, with entity name and field names used as table name and column names, respectively.

Example

@Entity

public class Employee {

    @Id

    private  int         employeeId;

    private  String      salutation;

    private  String      firstName;

    private  String      lastName;

    private  String      eMail;

    private  BigDecimal  salary;

    private  String      currency;

    private  int         version;

}

End of Content Area