Java Persistence API

from Wikipedia, the free encyclopedia

The Java Persistence API ( JPA ) is an interface for Java applications that simplifies the assignment and transfer of objects to database entries. It simplifies the solution to the problem of object-relational mapping , which consists of storing runtime objects of a Java application beyond a single session ( persistence ), whereby relational databases can be used that were not originally intended for object-oriented data structures.

The Java Persistence API was developed as a project of the JSR 220 Expert Group and was first published in May 2006. The specification of the current version 2.1 was released on April 22, 2013.

EclipseLink is the reference implementation for the Java Persistence API (JPA) 2.1 and 2.0. TopLink Essentials was the reference implementation for JPA 1.0.

Conception

In addition to the API, which is defined in the javax.persistence package , the Java Persistence consists of the following components:

Persistence Entity

A Persistence Entity is a Plain Old Java Object (POJO) , which is usually mapped to a single table in the relational database. Instances of this class correspond to the rows of the table. Depending on the design specifications, persistence entities can be implemented as simple data storage classes (comparable to a struct in C) or as business objects including business logic.

Object-relational metadata

The relationships between the individual tables are expressed using object-relational metadata. These are either created as Java annotations and / or stored in a separate XML file.

The Java Persistence Query Language

The Java Persistence Query Language ( JPQL ) is used to carry out queries regarding the entities stored in the database. These queries are syntactically similar to SQL queries, but refer to entities instead of database tables.

The JPA implementations transfer the queries formulated in JPQL at runtime into an SQL statement that is understood by the target database system. This abstraction allows the database system to be exchanged transparently, while the Java classes are completely retained. In contrast to this, the JPA also allows the use of "normal" SQL queries, these being referred to as native queries . When using native queries , however, the user must ensure that the query is understood by the target system.

Java Persistence in Context

Many Java developers were already using open source persistence frameworks before the Java Persistence API. This was mostly done on the grounds that the manual implementation of persistence was too complex and error-prone due to the object-relational impedance mismatch . The entity beans provided for this in the Java Platform, Enterprise Edition up to 1.4 are too expensive due to their high resource consumption, their complexity and the need to run on a Java EE application server. In contrast, the possibilities of the Java Data Objects propagated by SUN before the Java Persistence API were sufficient for very few developers.

During the development of the Java Persistence API, many properties of the established open source persistence frameworks such as Hibernate and Toplink were incorporated . Some of these frameworks now also offer implementations of the Java Persistence API.

Java Persistence was defined as part of the Enterprise JavaBeans 3.0 specification and is therefore a successor to Entity Beans. Although the EJB 3.0 specification is part of the Java EE 5 platform, no EJB container or a corresponding Java EE application server required. Future versions should therefore be defined as a separate Java Community Process outside of the EJB specification.

The Java Persistence API was developed for the relational persistence of relatively simple objects. For object databases , Java Data Objects or similar frameworks must still be used. The Service Data Object (SDO) API, on the other hand, is mainly used to map complex data to various formats and programming languages ​​for use in service-oriented architectures .

However, the Java Persistence API supports all three types of object-relational mapping of inheritance relationships ( table per inheritance hierarchy , table per subclass and table per specific class ).

Implementations

The JPA 2.0 specification is supported by a number of persistence frameworks , including Apache OpenJPA , Hibernate and EclipseLink .

literature

  • Bernd Müller, Harald Wehr: Java Persistence API 2. Hibernate, EclipseLink, OpenJPA and extensions . Carl Hanser Verlag, 2012, ISBN 978-3-446-42693-1 .

Web links

Wikibooks: Java Persistence  - learning and teaching materials (English)

Individual evidence

  1. EclipseLink 2.5 provides reference implementation for JPA 2.1 . heise.de. May 29, 2013. Retrieved January 4, 2014.
  2. Eclipse Announces EclipseLink Project to Deliver JPA 2.0 Reference Implementation . Eclipse Foundation. March 17, 2008. Retrieved July 27, 2008.
  3. ^ Gavin King, Christian Bauer, Emmanuel Bernard, Steve Ebersole: Hibernate Getting Started Guide. Accessed February 10, 2013 (English): “Working with both Object-Oriented software and Relational Databases can be cumbersome and time consuming. Development costs are significantly higher due to a paradigm mismatch between how data is represented in objects versus relational databases. "
  4. Java Feature - Inheritance Hierarchies in JPA