Java Persistence API

Java Persistence API

The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that allows developers to manage relational data in Java Platform, Standard Edition and Java Platform, Enterprise Edition applications.

The Java Persistence API originated as part of the work of the JSR 220 Expert Group.

Persistence consists of three areas:
* the API, defined in the javax.persistence package
* the Java Persistence Query Language
* object/relational metadata


A persistence entity is a lightweight Java class that typically represents a table in a relational database. Entity instances correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

The Java Persistence Query Language

The Java Persistence Query Language (JPQL) is used to make queries against entities stored in a relational database. Queries resemble SQL queries in syntax, but operate against entity objects rather than directly with database tables.

Relationship between Java Persistence API and Enterprise JavaBeans

The Java Persistence API was defined as part of the EJB 3.0 specification, which is itself part of the Java EE 5 platform. You do not need an EJB container or a Java EE application server in order to run applications that use persistence, however. Future versions of the Java Persistence API will be defined in a separate JSR and specification rather than in the EJB JSR/specification.

The Java Persistence API is a replacement persistence solution of EJB 2.0 CMP.

Relationship between Java Persistence API and Java Data Objects API

The Java Persistence API was developed in part to unify the Java Data Objects API, and the EJB 2.0 Container Managed Persistence (CMP) API. This seems to have been successful as most products supporting each of those APIs now support the Java Persistence API.

The Java Persistence API specifies only relational persistence (ORM) for Java (although there are providers that support other datastores). The Java Data Objects specification(s) provides relational persistence (ORM), as well as persistence to other types of datastores.

Relationship between Java Persistence API and Service Data Object API

The Java Persistence API is designed for relational persistence, with many of the key areas taken from object-relational mapping tools such as Hibernate and TopLink. It is generally accepted that the Java Persistence API is a significant improvement on the EJB 2.0 specification. The Service Data Objects (SDO) API (JSR 235) has a very different objective to the Java Persistence API and is considered complementary. The SDO API is designed for service-oriented architectures, multiple data formats rather than only relational data, and multiple programming languages. The Java version of the SDO API is managed via the JCP and the C++ version of the SDO API is managed via OASIS.

Motivation for creating Java Persistence API

Many enterprise Java developers have been using lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans because entity beans and enterprise beans were considered too heavyweight and complicated, and they could only be used in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and projects like Hibernate and Open-Source Version TopLink Essentials are now implementations of the Java Persistence API.

Relationship to Hibernate (Java)

Hibernate is the most commonly used Java persistence framework. The JPA was heavily contributed to by Hibernate authors, and is arguably much more like Hibernate than EJB 2. Hibernate is certified compliant with the Java Persistence API.


With Container Managed Transactions (CMT) in an EJB3 container, transaction demarcation is done in session bean annotations or deployment descriptors, not programatically. The EntityManager will automatically be flushed on transaction completion (and if you have injected or lookup the EntityManager, it will be also closed automatically).If an exception occurs during the EntityManager use, transaction rollback occurs automatically if you don't catch the exception. Since EntityManager exceptions are RuntimeExceptions they will rollback the transaction as per the EJB specification.If you work in a CMT environment, you might also want to use the same entity manager in different parts of your code. Typically, in a non-managed environment you would use a ThreadLocal variable to hold the entity manager, but a single EJB request might execute in different threads (e.g. session bean calling another session bean). The EJB3 container takes care of the persistence context propagation for you. Either using injection or lookup, the EJB3 container will return an entity manager with the same persistence context bound to the JTA context if any, or create a new one and bind it. In other words, all you have to do in a managed environment is to inject the EntityManager, do your data access work, and leave the rest to the container. Transaction boundaries are set declaratively in the annotations or deployment descriptors of your session beans. The life cycle of the entity manager and persistence context is completely managed by the container. The persistence context lifetime has transaction scope, so the persistence context ends when the transaction is committed or rolls back.Transaction management uses container-managed transactions with the required transaction attribute. This means that any business method will get invoked by the container in the context of a transaction (either an existing or a new one).our persistence context ends when the method returns because that is when the transaction ends. At this stage, the connection between all managed entities and the entity manager is removed and the entities change to the detached state.In the detached state, entity state is not synchronized with the database. So, how do we change the account so that the database is actually updated? We need to do two things: get a new persistence context, and transfer the entity to the managed state again. //CMT idiom through injection@PersistenceContext(name="PU Name") EntityManager em;

ee also

* DataNucleus Access Platform
* Hibernate
* OpenJPA
* TopLink
* EclipseLink

External links

* General info
** [ Sun's Persistence page]
** [ GlassFish's Persistence page]
** [ Documentation for the final version of the EJB3 spec (called JSR220)]
** [ Nabble JPA Forum]
* Documentation
** [ Persistence in the Java EE 5 Tutorial]
** [ Sun's Persistence FAQ]
** [ Java Persistence API Javadoc]
** [ Getting started with Java Persistence API 1.0]
* Implementations
** [ CocoBase] JPA Certified Commercial implementation for standalone and container services
** [ DataNucleus Access Platform] JPA/JDO Certified implementation of Java persistence
** [ JPOX] JPA/JDO Certified implementation of Java persistence
** [ GlassFish]
** [ SAP Netweaver Application Server, Java(TM) EE 5 Edition]
** [ TopLink]
** [ EclipseLink]
** [ Hibernate]
** [ OpenJPA]
** [ Kodo]
** [ Amber] (Part of Caucho Resin)
** [ SimpleJPA]
** [ Cayenne]
* Frameworks
** [ OpenXava] : JPA Application Engine: For rapid development of applications from JPA entities.
** [ Defrost] : JPA-based UI Framework: generates web forms from JPA entities (still under development).
* Articles
** [ Master the New Persistence Paradigm with JPA]
** [ Persistence Pays Offs: Advanced Mapping with JPA]

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Java Persistence API — (JPA)  API, входящий с версии Java 5 в состав платформ Java SE и Java EE, предоставляет возможность сохранять в удобном виде Java объекты в базе данных[1]. Существует несколько реализаций этого интерфейса, одна из самых популярных использует …   Википедия

  • Java Persistence API — Desarrollador Sun Microsystems Información general Género …   Wikipedia Español

  • Java Persistence API — Die Java Persistence API (auch JPA) ist eine Schnittstelle für Java Anwendungen, die die Zuordnung und die Übertragung von Objekten zu Datenbankeinträgen vereinfacht. Sie vereinfacht die Lösung des Problems der objekt relationalen Abbildung, das… …   Deutsch Wikipedia

  • Java Persistence API — La Java Persistence API (abrégée en JPA), est une interface de programmation Java permettant aux développeurs d organiser des données relationnelles dans des applications utilisant la plateforme Java. La Java Persistence API est à l origine issue …   Wikipédia en Français

  • Java Persistance API — Java Persistence API L’API de persistance Java des données, Java Persistence API abrégé en JPA, fait partie de la spécification EJB 3. Spécification EJB3 qui fait elle même partie de la plate forme JEE 5.0. La persistance des données en EJB3 est… …   Wikipédia en Français

  • Java Persistence Query Language — Испытал влияние: SQL Java Persistence Query Language (JPQL)  платформо независимый объектно ориентированный язык запросов являющийся частью Java Persistence API спецификации. JPQL используется для написания запросов к сущностям, хранящимся в …   Википедия

  • Java Platform, Enterprise Edition — or Java EE is a widely used platform for server programming in the Java programming language. The Java EE Platform differs from the Standard Edition (SE) of Java in that it adds libraries which provide functionality to deploy fault tolerant,… …   Wikipedia

  • Java Data Objects — (JDO) is a specification of Java object persistence. One of its features is a transparency of the persistent services to the domain model. JDO persistent objects are ordinary Java programming language classes; there s no requirement for them to… …   Wikipedia

  • Java EE — Java Platform, Enterprise Edition o Java EE (anteriormente conocido como Java 2 Platform, Enterprise Edition o J2EE hasta la versión 1.4), es una plataforma de programación parte de la Plataforma Java para desarrollar y ejecutar software de… …   Wikipedia Español

  • Java 2 Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”