Differences Between Hibernate and JPA

by Michael Scepaniak on December 9, 2008 in software development

Fans of the Hibernate object-relational mapping (ORM) framework will realize that the Java Persistence API (JPA) is basically a standardization of that framework. And, if you’re like me, you really haven’t given much thought to JPA, because, gee, isn’t it just a watered-down Hibernate? Maybe, maybe not. (I’m not going to get into that here and now.)

It can be useful, though (even if only politically or procedurally), to code to JPA instead of the Hibernate API. If you find yourself in that situation, you may find this compare/contrast between the two API’s to be useful:

Hibernate JPA
SessionFactory EntityManagerFactory
Session EntityManager
sessionFactory.getCurrentSession().[method]() entityManager.[method]()
saveOrUpdate() persist()
Query.setInteger/String/Entity() Query.setParameter()
list() getResultList()
uniqueResult() getSingleResult()
uniqueResult() returns null getSingleResult() throws NoResultException
CriteriaQueries – yes CriteriaQueries – no

Additionally, there are a couple Hibernate-specific JPA-isms to keep in mind:

  • If the underlying JPA implementation is Hibernate, either/both annotations or/and mapping files may be used – at the same time. In such a situation, I believe the mapping files act as an override for the annotations.
  • The best of both worlds (in my mind) is to base the code (at an interface- or API-level) on JPA and its EntityManager, but to have the implementation interact with the Hibernate Session, which can be obtained by calling getDelegate() on the EntityManager.
Mike
Want to be notified when new articles are posted?
Enter your email address:

{ 16 comments… read them below or add one }

Abdul jaleel October 12, 2011 at 12:55 pm

hi..

so you mean hibernate is more powerful that the jpa. ?

if so .. can you explain how ?
else how it is also ?

Reply

Michael Scepaniak October 12, 2011 at 7:13 pm

To a certain degree, yes. However, JPA is an API, whereas Hibernate in an implementation. They’re actually different animals and can’t be compared one to one. But, if you simply compare the methods made available by Hibernate’s Session and JPA’s EntityManager, you’ll see a lot more functionality in the Session object:
http://docs.jboss.org/hibernate/core/3.2/api/org/hibernate/Session.html
http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html

Whether or not the average developer needs that functionality is a different question, of course. And being an API that is intended to be implemented by various parties and vendors, it almost has to be watered-down and “generified”.

Thanks for the question, Abdul!

Reply

Manish Singh October 28, 2011 at 8:38 am

Hi Sir I agree with you @Michael But still mainly jobs comes on JPA in compresion to hibernate
Can you give some more features of hibernate compare to JPA.

Reply

Michael Scepaniak October 29, 2011 at 5:35 pm

As I said previously, JPA is an API that is intended to be implemented by various parties and vendors, of which Hibernate is one. Because of this, whatever JPA defines, Hibernate implements. There isn’t any comparison to be made in terms of features. Because Hibernate is an implementation of this standard (and more), Hibernate will have “more features”. Take a look at the documentation for more info.

Mike….

Reply

Senthil Muthiah March 6, 2013 at 12:46 am

Hi Mike

Thank you very much.

Do you have example of Hibernate with spring integration with latest version of hibernate and spring

Reply

Michael Scepaniak March 6, 2013 at 6:46 am

You’re welcome, Senthil.

No, I don’t. Not for the latest versions. Not with a clean, demonstrable integration just between Spring and Hibernate. Sorry.

Mike….

Reply

veeranna May 9, 2013 at 1:37 am

Thank you for the great explanation Mike. I’ve heard that JPA is portable across different technologies and hibernate is not. Can you explain this scenario..??

Reply

Michael Scepaniak May 11, 2013 at 9:01 pm

You’re welcome.

Well, since JPA is a specification and Hibernate is an implementation, that would theoretically make sense. However, whereas Hibernate has actually been ported to a non-Java language (see NHibernate), JPA hasn’t gone anywhere beyond Java.

Mike…

Reply

Ajay July 1, 2013 at 10:55 am

As per “Manish Singh” Query..let me clarify what he is asking for ?
The way you explained about JPA and the JPA providers is nice..

Let me add some more to “Manish Singh” question…If Hibernate is fine.Why companies ask the requirement on JPA(Along with some Java Technologies) rather than Hibernate ??

Why Companies ask the people who know JPA is eligible for the requirement ?

Thanks in Advance,

Best Regards,

Ajay

Reply

Michael Scepaniak July 1, 2013 at 8:51 pm

Ajay,

Not sure. I haven’t come across that requirement or, if I have, I haven’t paid attention to it. I’d chalk it up to lack of practical knowledge and an over-reliance on acronyms, specs, and buzzwords to sound smart. 😛

Mike….

Reply

Sowmya September 24, 2013 at 9:33 am

Hi Mike,

Very well explained about JPA and Hibernate but can you let me know like which is better for SOA architecture and why as we have TIBCO+Web services+JPA architecture in our current project.

Thanks,
Sowmya

Reply

sowmya October 1, 2013 at 6:25 am

Hi Mike,

Can we use Hibernate in SOA architecture? actually we have TIBCO+SOA+Web Services+JPA combination in our current project. So my doubt here is that they can use even hibernate right instead of JPA ?If so, why?What is the advantage of Hibernate over JPA in SOA architecture?

Thanks,
Sowmya

Reply

Michael Scepaniak December 19, 2013 at 7:54 pm

Sowmya,

The question that I’d have to ask of you is – what are you using as your JPA implementation? As I’ve stated previously (above, actually), JPA is an API that is intended to be implemented by various parties and vendors, of which Hibernate is one. As such, you should be able to use Hibernate with no problem.

Mike….

Reply

zoe August 22, 2015 at 4:09 am

Hi mike,

can you please tell me which entitymanager should i use to perform dao operation?

Thanks
zoe

Reply

Michael Scepaniak August 22, 2015 at 7:16 am

Zoe,

I’m sorry, but I don’t understand your question. Please elaborate. From which “entitymanager”s are you choosing?

Mike…

Reply

Kalaiselvan Velmurugab August 20, 2016 at 3:28 am

Thanks Mike.
I just had this doubt during past three years. I hope i got perfect clarification. once again thank u very much

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed

Previous post:

Next post:

Member of The Internet Defense League