Introduction of JPA (Java Persistence API)

The Java Persistence API, referred as JPA, is a Java programming language specification which describes the management of relational data in applications using Java Platform, Standard Edition and Enterprise Edition. JPA is designed for highly distributed applications, Especially web-enabled application. JPA was 1.0 introduced in Java EE 5 and JPA 2.0 is part of Java EE 6 standards.

The Java Persistence API provides a POJO persistence model for object-relational mapping. JPA simplifies development and provide framework and tools around providing automatic persistence. In JPA objects are mapped to tables using metadata, Metadata is used to transform data from one representation to another. Life cycle of JPA is manageable by application server to increase quality of service.

The Java Persistence API was developed by the EJB 3.0 software expert group as part of JSR 220, but its use is not limited to EJB software components. It can also be used directly by web applications and application clients, and even outside the Java EE platform.

The final release of JPA 1.0 specification was released on 11 May 2006 and JPA 2.0 specification was released on 10 Dec, 2009. The JPA 2.1 specification was released 22 April, 2013.

JPA standardized the ORM persistence technology for Java developers. JPA is not a product and can not be used as it is for persistence. It needs an ORM implementation to work and persist the Java Objects. ORM frameworks that can be used with JPA are Hibernate, Toplink, Open JPA etc.


JPA 2.0 Features

The main features included in JPA 2.0 update are :

  • Object-relational mapping functionality is expanded -
    • Support for collections of embedded objects, linked in the ORM with a many-to-one relationship
    • Multiple levels of embedded objects
    • Ordered lists
    • Combinations of access types
  • A criteria query API
  • Standardization of query 'hints'
  • Standardization of additional metadata to support DDL generation
  • Support for validation

Benefits of JPA

  • Simplified Persistence technology
  • ORM frameworks independence: Any ORM framework can be used
  • Data can be saved in ORM way
  • Supported by industry leaders

ORM frameworks

Here are the list of ORM frameworks that can be used with JPA specification.

  • Hibernate
  • Toplink
  • iBatis
  • Open JPA

Why JPA?

  • JPA is standardized specification and part of EJB3 specification
  • Number of free ORM frameworks are available which can be used to develop applications of any size
  • Application developed in JPA is portable across many servers and persistence products (ORM frameworks)
  • Can be used with both JEE and JSE applications
  • Annotations can be used with JPA
  • Both annotations and xml based configuration support

The persistence.xml file is a standard configuration file in JPA. It has to be included in the META-INF directory that contains the entity beans. The persistence.xml file must define a persistence-unit with a unique name in the current scoped classloader. The provider attribute specifies the underlying implementation of the JPA EntityManager. In JBoss Application Server, the default and only supported / recommended JPA provider is Hibernate.

You have to specify the persistence provider element which you are using, in persistence.xml file. If you are using Hibernate persistence then then you must specify the provider element as follows :

<provider> org.hibernate.ejb.HibernatePersistence </provider>


What Do You Need to Develop with JPA

To start developing with JPA, you need the following :

  • Relational Database
  • Domain Model Classes
  • persistence.xml File
  • Object Relational Mapping Metadata
  • Persistence Provider

Relational Database

There are a number of relational databases available in the market. To develop your applications with JPA, you can use any relational database.


Domain Model Classes

Your domain model should consist of classes representing entities-lightweight persistent domain objects. The easiest way to define an entity class is by using the @Entity annotation, as given below :

@Entity
 public class Techknow {
 ...
 ...
 }

persistence.xml File

Use the persistence.xml file to package your entities. Persistence units are defined in a persistence.xml file, which has to be located in the META-INF directory in the classpath.

Structure of the persistence.xml file is given below, which defines a single persistence unit :

<?xml version="1.0" encoding="UTF-8"?>
 
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/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">
     
    <persistence-unit name="myunit"
    	transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence
        	</provider>
         
    <properties>
        <property name="hibernate.show_sql"
        	value="true"/>
        <property name="javax.persistence.jdbc.driver"
        value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://localhost:3306/emp_db"/>
        <property name="javax.persistence.jdbc.user"
        value="root" />
        <property name="javax.persistence.jdbc.password"
        value="root" />
        <property name="hibernate.dialect"
        value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto"
        value="create"/>
        </properties>
    </persistence-unit>
    
</persistence>

Here we use Hibernate Persistence that why in provider tag HibernatePersistence is define.


Object Relational Mapping Metadata

Object Relational Mapping Metadata specifies the mapping of your domain model classes to the database. You can express this metadata in the form of annotations and/or XML.

  • Metadata Annotations

    A metadata annotations represents a Java language feature that lets you attach structured and typed metadata to the source code. Annotations alone are sufficient for the metadata specification, you do not need to use XML. Annotations for object relational mapping are in the javax.persistence package.

    An object relational mapping XML file is optional. If you choose to provide one, then it should contain mapping information for the classes listed in it. The persistence provider loads an orm.xml file (or other mapping file) as a resource. If you provide a mapping file, the classes and mapping information specified in the mapping file will be used.

  • Overriding Annotations with XML

    XML mapping metadata may combine with and override annotation metadata.


Advantages and Disadvantages of Using Annotations

Metadata annotations are relatively simple to use and understand. They provide in-line metadata located with the code that this metadata is describing-you do not need to replicate the source code context of where the metadata applies.
On the other hand, annotations unnecessarily couple the metadata to the code. Thus, changes to metadata require changing the source code.


Advantages and Disadvantages of Using XML

Followings are some advantages of using XML :

  • No coupling between the metadata and the source code.
  • Support in IDEs and source control systems.
  • Compliance with the existing, pre-EJB 3.0 development process
The main disadvantages of mapping with XML are the complexity and the need for replication of the code context.



Persistence Provider

The persistence provider supplies the implementation of the JPA specification. The persistence provider handles the object relational mapping of the relationships, including their loading and storing to the database, and the referential integrity of the relationships.