miracle occurs here|

Differences Between Hibernate and JPA

December 9th, 2008 at 8:02 pm by Michael Scepaniak

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.

4 Comments

trackback
  1. Abdul jaleel Says:

    hi..

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

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

  2. Michael Scepaniak Says:

    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!

  3. Manish Singh Says:

    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.

  4. Michael Scepaniak Says:

    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….

 Follow comments for this post

Leave a Comment