+

Search Tips   |   Advanced Search

Java Persistence API (JPA) architecture

Data persistence is the ability to maintain data between application executions. Persistence is vital to enterprise applications because of the required access to relational databases. Applications developed for this environment must manage persistence themselves or use third-party solutions to handle database updates and retrievals with persistence. The Java Persistence API (JPA) provides a mechanism for managing persistence and object-relational mapping and functions for the EJB specifications.

The JPA specification defines the object-relational mapping internally, rather than relying on vendor-specific mapping implementations. JPA is based on the Java programming model that applies to Java EE environments, but JPA can function within a Java SE environment for testing application functions.

JPA simplifies the persistence programming model. The JPA specification explicitly defines the object-relational mapping, rather than relying on vendor-specific mapping implementations. JPA standardizes the important task of object-relational mapping using annotations or XML to map objects into one or more tables of a database. To further simplify the persistence programming model:

JPA is designed to operate both inside and outside of a Java Enterprise Edition (Java EE) container. When we run JPA inside a container, the applications can use the container to manage the persistence context. If there is no container to manage JPA, the application must handle the persistence context management itself. Applications designed for container-managed persistence do not require as much code implementation to handle persistence, but these applications cannot be used outside of a container. Applications that manage their own persistence can function in a container environment or a Java SE environment.

Java EE containers that support the EJB 3.x programming model must support a JPA implementation, also called a persistence provider. A JPA persistence provider uses the following elements to allow for easier persistence management in an EJB 3.x environment:

Persistence unit

Consists of the declarative metadata that describes the relationship of entity class objects to a relational database. The EntityManagerFactory uses this data to create a persistence context that can be accessed through the EntityManager.

EntityManagerFactory

Used to create an EntityManager for database interactions. The application server containers typically supply this function, but the EntityManagerFactory is required if we are using JPA application-managed persistence. An instance of an EntityManagerFactory represents a Persistence Context.

Persistence context

Defines the set of active instances that the application is manipulating currently. The persistence context can be created manually or through injection.

EntityManager

The resource manager that maintains the active collection of entity objects that are being used by the application. The EntityManager handles the database interaction and metadata for object-relational mappings. An instance of an EntityManager represents a Persistence Context. An application in a container can obtain the EntityManager through injection into the application or by looking it up in the Java component name-space. If the application manages its persistence, the EntityManager is obtained from the EntityManagerFactory.

Injection of the EntityManager is only supported for the following artifacts:

  • EJB 3.x session beans
  • EJB 3.x message-driven beans
  • Servlets, Servlet Filters, and Listeners
  • JSP tag handlers which implement interfaces javax.servlet. jsp.tagext.Tag and javax.servlet.jsp.tagext.SimpleTag
  • JavaServer Faces (JSF) managed beans
  • the main class of the application client.

Entity objects

A simple Java class that represents a row in a database table in its simplest form. Entities objects can be concrete classes or abstract classes. They maintain states using properties or fields.


Subtopics

  • IBM Optim pureQuery Runtime