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.
Persistence unit
This is the logical grouping for achieving the persistence defined in persistence.xml. The persistence unit is defined by multiple persistence classes (Entity classes and so on) and O/R mapping files.
Entity
An Entity is a Java class that is associated with a database table. An instance of this Java class corresponds to one database record.
Persistence context
This is a set of Entity class instances. JPA performs operations in relation to persistence contexts, and achieves database synchronization by transaction segmentation.
Java Persistence Query Language (JPQL)
This is a query language that is equivalent to database SQL.
Entity manager
This is the interface for Entity and persistence context operations using JPQL.
persistence.xml
The Entity class, O/R mapping file, transaction types, and other configuration information can be defined in persistence units.
orm.xml
This is the O/R mapping file. The O/R mapping file codes the associations set between Entities and databases. This file is not mandatory, since these can also be coded using annotations.
The EJB 2.1 entity bean and the JPA Entity have the following major differences:
File that configures the Entity
EJB 2.1 entity bean conventions require that an entity bean class, a HOME interface, and a Component interface be created, and the entity bean must be defined in the deployment descriptor.
With a JPA Entity, simply create a Java class for the Entity and set O/R mapping information of @Entity annotations, and so on, in that Java class.
Entity invocation method
In Java EE, Dependency Injection can be used for JPA Entities and also to easily code conventional lookup processing.
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); } |