Sunday, October 4, 2009

JPA Introduction, What is JPA, Java Persistence API tutorial

Java Persistence API (JPA) provides POJO (Plain Old Java Object) standard and object relational mapping (OR mapping) for data persistence among applications. Persistence, which deals with storing and retrieving of application data. One of the great benefits of JPA is that it is an independent API and can nicely integrate with J2EE as well as J2SE applications.

A fundamental question for many Java developers is "Why JPA? Why do I need to know how to use this API when object-relational mapping tools like Hibernate and Toplink are already available?" The answer is that JPA is not a new technology; rather, it has collected the best ideas from existing persistence technologies like Hibernate, TopLink, and JDO. The result is a standardized specification that helps you build a persistence layer that is independent of any particular persistence provider.

Although it all started with entity beans and is packaged with Java EE 5.0, JPA can be used outside the container in a Java SE environment.

What is JPA?
JPA is just an specification from Sun, which is released under JEE 5 specification. JPA standardized the ORM persistence technology for Java developers. JPA is not a product and can't 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.

These days most of the persistence vendors are releasing the JPA implementation of their persistence frameworks. So, developers can choose the best ORM implementation according to the application requirement. For example, production can be started from the free versions of ORM implementation and when the needs arise it can be switched to the commercial version of the ORM framework. You can switch the persistence provides without changing the code. So, ORM framework independence is another another big benefit of JPA.

Here are the benefits of JPA
1. Simplified Persistence technology
2. ORM frameworks independence: Any ORM framework can be used
3. Data can be saved in ORM way
4. 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?
1. JPA is standardized specification and part of EJB3 specification
2. Many free ORM frameworks are available with can be used to develop applications of any size 3. Application developed in JPA is portable across many servers and persistence products (ORM frameworks).
4. Can be used with both JEE and JSE applications
5. JSE 5 features such as annotations can be used
6. Both annotations and xml based configuration support
source: http://roseindia.net/jpa/jpa-introduction.shtml

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 do not have to specify the persistence provider if you’re using the default persistence provider integrated with your Java EE 5 container. For example, if you want Hibernate’s persistence provider in the JBoss Application Server or TopLink Essentials persistence provider with Sun GlassFish or the Oracle Application Server, you don’t have to define the provider element in persistence.xml. But if you decide to go with the EJB 3 persistence provider from the GlassFish project with either JBoss or Apache Geronimo, then you must specify the provider element as follows:

<provider>oracle.toplink.essentials.PersistenceProvider</provider>

Obviously this example specifies Oracle TopLink as the persistence provider; you can specify the provider element for Hibernate as follows:

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

This is helpful when using JPA outside the container.


Template of persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="pu1"> <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider> <class>entity.Customer</class> <class>entity.Order</class> <properties> <property name="toplink.jdbc.driver" value="<database driver>"/> <property name="toplink.jdbc.url" value="<database url>"/> <property name="toplink.jdbc.user" value="<user>"/> <property name="toplink.jdbc.password" value="<password>"/> <property name="toplink.logging.level" value="INFO"/> </properties>
</persistence-unit>
</persistence>

7 comments:

  1. Really you have very nicely explained why did JPA come into picture:) too good :)

    ReplyDelete
  2. I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

    ReplyDelete
  3. It is a great explanationa about why JPA is indroduced. Thanks.

    ReplyDelete
  4. Indeed a good explanation :)

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)