Java Persistance API (JPA) is the object relational mapping (ORM) specification for Java EE applications. It is only the specification, every framework owners can implement their own persistence layer following the JPA specification. Standardizing three major competing vendors of ORM technologies, Hibernate, JDO and Toplink, into one standard is the JPA 1.0 specification.
JPA 1.0 has covered all the essential features needed for the ORM framework and got the industry acceptance for the specification. JPA 2.0 comes with extra and nice to have features of ORM framework which is missed out in the JPA 1.0 and used in the popular ORM frameworks. This tutorial points out the new features introduced in the JPA 2.0 package. JPA 2 is released on december, 2009.
JPA 2.0 New Features
1. Criteria API
JPA 2.0 has introduced Criteria API for eliminating the use of JPA Query Language (JQL). If you are a SQL developer, you can leverage your SQL knowledge to fine tune the queries by writing more advanced JQL. But, in the Object Oriented world, it has several drawbacks like compile time checking of the queries. If you are using JQL, the first time it is checked is at the run time. It has lot of problem in the performance of developers. With the Criteria API, you can write your queries using Java code. The erros are captured at the compile time. Now with the JPA criteria API it’s possible to have type safe queries that can be checked at compile time.
There are two main classes you have to use for building the queries.
You can get these objects using the entity manager object as below.
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cqry = entityManager.createQuery();
Read More About This Topic:
2. Bean Validation
Bean validation is an optional feature introduced in JPA 2.0, supported with its persistence provider. The persistence provider enables Bean validation on all entities to which annotations are applied. Bean validation can be disabled by setting the validation-mode element in persistence.xml or the javax.persistence.validation.mode property to none. Implementing the bean validation is very simple. You have to annotate with the javax.validation.constraints annotations to the fields which requires the validations. If the fields are failed in the validation, the values are not presosted to the back end.
3. JDBC Properties
There are number of properties defined in the persistence.xml become the standard of JPA specification. In JPA 1., these properties are used as the vendor specific, JPA 2 makes it the standard. Some of the importance properties are:
- javax.persistence.jdbc.driver – fully qualified name of the driver class
- javax.persistence.jdbc.url – driver-specific URL
- javax.persistence.jdbc.user – username used by database connection
- javax.persistence.jdbc.password – password for database connection validation
The sample persistence.xml would look like this:
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="test" transaction-type="RESOURCE_LOCAL"> <class>javabeat.net.jpa.TestClass</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="admin" /> </properties> </persistence-unit> </persistence>
There are lot of changes in the mappings which are not commonly used in all the applications. Initial version of JPS has covered most of the mappings and associations, but JPA 2 has dramatically improved the mappings by adding the more numbers of combinations which are useful for using with the legacy systems. Here is the quick list of changes.
- Element Collections
- Adding a Join Table
- Unidirectional One-to-Many With No Join Table
- Orphan Removal
- Persistently Ordered Lists
- Derived Identifiers
5. Shared Cache
Normally entities can be cached in the persistence layer to improve the performance. If the entities are long lived, it can be stored in the persistence layer to share among all other context. This is known as the shared cache mechanism. This approach is made a standard from the JPA 2. The standard cache interface can be invoked from EntityManagerFactory.getCache() method. A shared-cache-mode element can be configured in the persistence.xml file for a given persistence unit.
6. JPA 2.0 Support
The following ORM framework vendors are supporting the JPA 2.0 specification.
- DataNucleus (formerly JPOX)
- EclipseLink (formerly Oracle TopLink)
- JBoss with Hibernate
- IBM, for WebSphere Application Server
- Versant Corporation JPA