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

Entities

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.

@PersistenceContext

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
* JPOX
* OpenJPA
* TopLink
* EclipseLink

External links

* General info
** [http://java.sun.com/javaee/technologies/entapps/persistence.jsp Sun's Persistence page]
** [http://glassfish.dev.java.net/javaee5/persistence/ GlassFish's Persistence page]
** [http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html Documentation for the final version of the EJB3 spec (called JSR220)]
** [http://www.nabble.com/JPA-f27109.html Nabble JPA Forum]
* Documentation
** [http://java.sun.com/javaee/5/docs/tutorial/doc/?wp406141&JavaEETutorialPartFour.html#wp996871 Persistence in the Java EE 5 Tutorial]
** [http://java.sun.com/javaee/overview/faq/persistence.jsp Sun's Persistence FAQ]
** [http://java.sun.com/javaee/5/docs/api/javax/persistence/package-summary.html Java Persistence API Javadoc]
** [http://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/40ff8a3d-065a-2910-2f84-a222e03d1f43 Getting started with Java Persistence API 1.0]
**
* Implementations
** [http://www.thoughtinc.com CocoBase] JPA Certified Commercial implementation for standalone and container services
** [http://www.datanucleus.org/ DataNucleus Access Platform] JPA/JDO Certified implementation of Java persistence
** [http://www.jpox.org/ JPOX] JPA/JDO Certified implementation of Java persistence
** [http://glassfish.dev.java.net GlassFish]
** [http://www.sdn.sap.com/irj/sdn/javaee5 SAP Netweaver Application Server, Java(TM) EE 5 Edition]
** [http://www.oracle.com/technology/products/ias/toplink/index.html TopLink]
** [http://www.eclipse.org/eclipselink/ EclipseLink]
** [http://www.hibernate.org/ Hibernate]
** [http://openjpa.apache.org/ OpenJPA]
** [http://www.bea.com/kodo/ Kodo]
** [http://www.caucho.com/resin-3.1/doc/amber.xtp Amber] (Part of Caucho Resin)
** [http://code.google.com/p/simplejpa/ SimpleJPA]
** [http://cayenne.apache.org/ Cayenne]
* Frameworks
** [http://www.openxava.org/ OpenXava] : JPA Application Engine: For rapid development of applications from JPA entities.
** [http://sourceforge.net/projects/defrost/ Defrost] : JPA-based UI Framework: generates web forms from JPA entities (still under development).
* Articles
** [http://www.devx.com/Java/Article/33650 Master the New Persistence Paradigm with JPA]
** [http://www.devx.com/Java/Article/33906 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 http://java.sun.com/javaee/... 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”