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.
Leave a comment
16 comments
hi..
so you mean hibernate is more powerful that the jpa. ?
if so .. can you explain how ?
else how it is also ?
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!
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.
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....
Hi Mike
Thank you very much.
Do you have example of Hibernate with spring integration with latest version of hibernate and spring
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..??
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
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
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
Hi mike,
can you please tell me which entitymanager should i use to perform dao operation?
Thanks
zoe
Thanks Mike.
I just had this doubt during past three years. I hope i got perfect clarification. once again thank u very much