Top
Interstage Studio User's Guide
FUJITSU Software

4.1.1 What is JPA?

Java Persistence API (JPA) groups together the EJB 2.1 entity bean specifications as one independent specification from EJB 3.0. JPA provides object/reference (O/R) mapping mechanisms in order to operate relational databases (RDB) from Java applications. O/R mapping maps the Java persistence class and RDB tables, thereby making it easy to make data persistent. Since JPA handles databases as Java objects, the number of situations where SQL statements are issued to databases can be reduced.

Simple explanations are given below for the terminology required to understand JPA.

4.1.1.1 Modifications since EJB2.1

The EJB 2.1 entity bean and the JPA Entity have the following major differences:

Simple examples of the main annotations used by JPA are introduced below.

javax.persistence.Entity Annotation

This annotation defines a class as an Entity. The following element can be specified.

Element Name

Explanation

name

The Entity name. If omitted, the class name is used as the Entity name.

Example of using Entity annotation

package sample;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
      @Id
      private int     id;
      private String  name;
      private String  address;
      private String  tel;
}

javax.persistence.PersistenceContext Annotation

An Entity is managed by the Entity manager. The PersistenceContext annotation can obtain this Entity manager through Dependency Injection. The following elements can be specified.

Element Name

Explanation

name

This is the name used to access the Entity manager in an environment that references a context. This name is not used with Dependency Injection.

properties

Use this element if you want to specify properties for a container or a persistence provider.

type

Specify the transaction type.

unitName

Persistence unit name. Specify the name that can be accessed using JNDI.

Example of using PersistenceContext annotation

@PersistenceContext
EntityManager em;

public Employee getEmployeeByPrimaryKey (int id) {
    return em.find(Employee.class, id);
}