December 17 2012 by Kevin Bowersox

This morning, I revisited a stackoverflow question, I answered awhile back.  I wanted to provide a more complete answer and also saw the opportunity for a new post.  The question asked, "What's the difference between JPA and Hibernate?"

The Java Persistence Architecture API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database. 

Let's take a further look at this definition.  As the API portion of the name implies, JPA is a specification, meaning it provides guidelines for developing an interface that complies with a certain standard.  While JPA dictates an interface, it does not provide an implementation of that interface, meaning there is no underlying code that performs the operations to persist an object to a relational database.

It should also be noted the term Object Relational Mapping, is often used to describe the process of accessing, persisting and managing data between Java objects and a relational database.

To look at the concept of JPA from another perspective, imagine if you were provided this interface.

public interface JPA {
	
	public void insert(Object obj);
	
	public void update(Object obj);
	
	public void delete(Object obj);
	
	public Object select();
	
}

Without further development, what immediate value does this interface provide?  While this interface has potential to provide value, at this point, very little value is provided because the interface lacks an implementation.  If this interface is used within any code it will not execute because no concrete objects that implement this interface exist to perform the work.  This same concept applies to JPA just on a larger scale since the API specification defines many interfaces and annotations.

This is where the role of the JPA provider comes into play.  JPA providers develop a JPA implementation that meets the requirements of the JPA specification.  Hibernate is a JPA Provider, as well as others such as EclipseLink and TopLink.  With a JPA implementation in place Java objects can be now be persisted to a relational database, since there is underlying code to perform the work.

Returning to our interface analogy, if JPA is the interface then Hibernate represents a class that implements the interface.

public class Hibernate implements JPA {

	public void insert(Object obj) {
       //Persistence code
	}

	public void update(Object obj) {
	   //Persistence code
		
	}

	public void delete(Object obj) {
	  //Persistence code
		
	}

	public Object select() {
	    //Persistence code
	}
	
	public Object superSelect(){
		//Persistence Code
	}
}

Notice that in addition to implementing the JPA interface, the Hibernate class contains some methods superfluous to the interface.  Keep this in mind for later.  Given this JPA implementation, we could now write some code that relies upon it to persist some data to a relational database.

public class MyApplication {

	public static JPA jpa = new Hibernate();
	
	public static void main(String[] args) {
		Object object = new Object();
		jpa.insert(object);  //writes to DB
	}
}

The new application works well initially, but after a couple of months its performance degrades.  Let's assume that the Hibernate implementation behind the scenes has several deficiencies causing  the poor performance.  Remember, this is for example purposes and I judging the merit of Hibernate.

Upon encountering this issue, another provider may decide the need for another JPA implementation exists.  This provider creates their own implementation of the JPA specification and publishes the code.

public class ToThoughtJpa implements JPA {
	//Implementation
}

Having used the JPA interface in our application, we can now easily make the switch to the more reliant JPA implementation.

public class MyApplication {

	public static JPA jpa = new ToThoughtJpa();
	
	public static void main(String[] args) {
		Object object = new Object();
		jpa.insert(object);  //writes to DB
	}
}

The concept illustrated in our simple example is the main value JPA provides only on a much larger scale.  If we choose to use JPA, we can eventually switch out our chosen JPA implementation for another implementation as long as they both meet the JPA specification.  In reality, this is not always a seamless transition, since we often utilize features of the implementation that are not support by the specification and each implementation has its own little quirks.  To illustrate this point, consider if we had called the superSelect method within our application.

public class MyApplication {

	public static Hibernate jpa = new Hibernate();
	
	public static void main(String[] args) {
		Object object = new Object();
		jpa.insert(object);  //writes to DB
		jpa.superSelect();
	}
}

Notice that in order to call the method, the interface of type JPA must be replaced with the Hibernate implementation.  At this point, we cannot swap our JPA implementation to the ToThoughtJpa JPA implementation because its interface does not contain the superSelect method.  This example attempts to illustrate the restrictions that occur when a developer chooses to use the straight Hibernate implementation, which is not bound by the JPA specification.

In summary, JPA is not an implementation, it will not provide any functionality within your application.  Its purpose is to provide a set of guidelines that can be followed by JPA providers to create an ORM implementation in a standardized manner.  This allows the underlying JPA implementation to be swapped and for developers to easily transition (think knowledge wise) from one implementation to another.  Hibernate is arguably the most popular JPA provider.  Hibernate's JPA implementation is used by many developers, however some choose to use the actual Hibernate implementation itself because the implementation may contain advanced functionality not contained in the JPA implementation.

Comments
Post a Comment
Eric commented on June 18 2013 9:52 AM
Thanks a lot !! Your explanations are very clear.
Sharath commented on June 18 2013 8:21 AM
Thank u, it clears lot of doubt.
Email
lakshmi commented on June 11 2013 2:46 AM
Very good explanation and it cleared my doubts. Thank you.
Abu shalihu commented on June 05 2013 7:39 AM
Well explained!
Sujith commented on June 03 2013 10:02 AM
Nice article... helped me to clear my doubts..
Email
Sandeep jaiswal commented on May 29 2013 15:55 PM
I mean this is my 1st day on JPA/Hibernate and I think , I am damn clear what it's all about. Super beginning thanks alot
Email
Nitin commented on May 26 2013 4:54 AM
Thanks for explaining so well...I got my doubt cleared.
Madhab commented on May 22 2013 13:54 PM
Very well explained, thanks
Sumit commented on May 21 2013 10:14 AM
Awesome...thanks looking forward for answers of some more questions
Email
Manoj K. Ghanaksh commented on May 09 2013 17:12 PM
Good Article, clear my confusion
Email
Yali Wu commented on May 05 2013 20:10 PM
Thank you for the great article, very easy to follow and well explained!
Wanderer commented on April 25 2013 13:38 PM
Do you mean JPA alone is not enough?
Munim commented on April 25 2013 10:27 AM
Well explained to the point.
John C. commented on April 23 2013 22:35 PM
Thanks for the clear explanation. I've just started reading about ORM and found this article nicely lays out the pieces for someone like me with no experience in this area. I would be interested in your take on the relationship of Spring and iBatis to JPA and Hibernate (given that a relationship exists).
Mike commented on April 18 2013 14:20 PM
Nice article, thanks!
Ka Wing commented on April 17 2013 7:12 AM
Very nice article
soufiane commented on April 12 2013 8:32 AM
It was clear and concise. Thanks you :)
Email
Mayank commented on April 11 2013 5:44 AM
Thankyou!
Chris B commented on April 09 2013 13:36 PM
Clear and concise. Thanks a lot, Kevin!
Saurabh commented on April 04 2013 2:52 AM
Awsome....
Balaji Mohan commented on March 29 2013 17:45 PM
Thanks buddy for the illustration. Helps a beginner like me.
vinit commented on March 19 2013 11:34 AM
Good Explaination.
Ali Hammad commented on March 19 2013 10:41 AM
Does Oracle its self provide any implementation of JPA?
Ali Hammad commented on March 19 2013 10:40 AM
Does Oracle provide any implementation of JPA as well?
Girish.. commented on March 03 2013 5:36 AM
Very Nice.. Keep it up...
Email
Lukas commented on February 24 2013 15:21 PM
Very nice article, you've made it very clear and easy to understand.
Email
Universe commented on February 24 2013 7:58 AM
Very Nice Article. Crisp and clear
Diego Ruiz commented on February 22 2013 14:49 PM
Very clear. Thank you.
Email
lak commented on February 18 2013 4:29 AM
Good article. Thanks.
onepotato commented on February 14 2013 3:21 AM
Simple, clear, straight to the point. Really helpful for a junior like me. Thank you.
Hitesh commented on February 13 2013 21:44 PM
Simple, easy and to the point. Well done. Thanks.
Tahir Hussain commented on February 13 2013 16:12 PM
two cents, well spent.
Email Site
Idrees Hamayun commented on February 10 2013 15:06 PM
this was explained in a very easy and understandable manner and it helped me alot to understand concepts.Thanks very Much.
Ahmed Adel commented on February 08 2013 13:37 PM
This is a very simple and useful topic. You really helped me to understand things that confused me for long time Thank you very much and wish you best luck
Email
Prakash S commented on February 07 2013 6:50 AM
Thankyou very much for providing such an easy-to-understand explanation.
Diego Ramos commented on February 03 2013 22:07 PM
Thank you very much! very clear and understandable! It should have more comments! I always had this doubt but now you made it clear:)
Stefan commented on January 23 2013 18:59 PM
Very good article. Thank you !
Bob M commented on January 16 2013 13:58 PM
Great article. Very easy to understand and follow.
Post a Comment
*Name
Email
Site
*Comment