Search code examples
javahibernatejpajakarta-eejpa-2.0

What are the differences between Hibernate and JPA?


When I was in college learning about web programming, they told us about Hibernate.

We used it for a while, I even had the chance to work with it in a real scenario in a company for almost 8 months. Now that I completely switched to Java EE 6, I use JPA for my ORM needs.

It has been a few months since I use it, but I don't really understand what are the differences between one and other. Why some people say one or other is better or worse? The way I do my mappings and annotations in both is almost the same.

Maybe you can solve some of my doubts:

  • What are the advantages and disadvantages of each?

  • Does Hibernate uses JPA or the other way around (do they depend on each other)?

  • From the point of view of features, what features does one have that does not have the other?

  • Any other differences between both?


Solution

  • JPA (Java Persistence API) is an API, JPA 2.0 is of the JSR 317 group. Basically it's a framework to manage relational data by using ORM (Object Relational Mapping) for data persistence.

    Hibernate is a ORM library that maps your POJO/JavaBeans to your data persistence. Both ORM and JPA have a object/relational metadata (XML, Annotations) for mapping POJO's to DB tables.

    Does Hibernate uses JPA or the other way around (Do they depend on each other)?

    Hibernate 3 now supports JPA 2.0. JPA is a specification describing the relation and management of relational data using Object model. Since JPA is an API, Hibernate implements this API. All you have to do is write your program using JPA API classes/interfaces, configure Hibernate as a JPA resource and voila, you got JPA running.

    What are the advantages and disadvantages of each?

    Advantages:

    • Avoids low level JDBC and SQL code.
    • It's free (EclipseLink e.g. for JPA).
    • JPA is a standard and part of EJB3 and Java EE.

    That's all I know of Hibernate.