- 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 Editionand Java Platform, Enterprise Editionapplications.
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 XMLdescriptor 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
SQLqueries 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 Objectsspecification(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 mappingtools 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.
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;
* 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]
** [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]
** [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]
** [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).
** [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.