11

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
javing
  • 12,307
  • 35
  • 138
  • 211

6 Answers6

8

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • If Java EE 5 only supports JPA 1.0, can I use the latest version of HIbernate (4.1.x) in a Java EE 5 environment? – Robert Jul 12 '12 at 20:17
  • 1
    This answer does not make clear, that JPA does not work without an implementation which could even be Hibernate. – sorencito Aug 13 '13 at 09:58
7

JPA is a an API standard for persistence mapping. It was developed long after Hibernate and heavily influenced by it.

To use JPA, you need a JPA implementation. Hibernate is one such JPA implementation, since it was extended to implement the API it had inspired.

Additionally, Hibernate offers some features that are not offered by the JPA standard, but these are usually not needed.

On the other hand, if you use only JPA features, your code should work with a different JPA implementation as well — experience shows that compatibility is rarely 100%, but switching to a different implementation should not require a lot of work.

stites
  • 4,903
  • 5
  • 32
  • 43
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

JPA is a standard, implemented by many different libraries, like TopLink for example. Hibernate is a library which exists long before JPA and has some features that JPA doesn't have.

The Criteria api is in both, but I think Hibernate's is much simpler to use. So, if you use JPA, you can switch to a different implementation without problems, or you may have a base project which defines base JPA dao and two different projects which use a different implementation.

If you use Hibernate it is probably easier to find examples and experts.

Riccardo Cossu
  • 2,699
  • 1
  • 28
  • 47
1

Hibernate was the inspiration for the new-modern JPA. EJB 3.0 ORM (JPA) is based on simple, clean, beautiful Hibernate.

When JPA was born, Hibernate became JBoss' JPA implementation base. So if you are using JBoss and JPA, you are probably using Hibernate underneath.

I can't spot any significant advantage of one over the other. Maybe the main thing is that if you use JPA then you are using a pure JavaEE implementation. Meaning that, in theory, you could switch to a different application server and your application will still work even if the new application server is not using Hibernate. Keep in mind that I said IN THEORY. :-)

JPA uses Hibernate in some application servers. RedHat's JBoss is the main product using Hibernate for its JPA implementation.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • About when you mention "JPA uses Hibernate in some application servers" I am curious, what about GlassfishV3.0 Does it use Hibernate? I use Glassfish and i think sometimes i saw some message related to hibernate on the console, when building or doing some mappings. – javing Apr 15 '11 at 15:41
  • @sfrj I don't think Glassfish uses Hibernate. I think it uses a library called TopLink (belongs to Oracle). – Pablo Santa Cruz Apr 15 '11 at 15:53
  • I might be mistaken so, i thought i saw the word hibernate a couple of times in the console :) – javing Apr 15 '11 at 16:04
  • Let's be clear; any application server can you *any* JPA implementation. It may come bundled with one that was developed by the JEE vendor, but that doesn't necessarily mean best for that application and you can use the implementation you choose. – DataNucleus Apr 17 '11 at 09:18
0
interface JPA {
    void merge(Object o);
}

class Hibernate implements JPA {
    @Override
    public void merge(Object o) {
        // Implementation here
    }
}
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Kyll Aug 07 '15 at 19:56
0

I will try to explain as simply as possible.

-What are the advantages and disadvantages of each?

JPA properties are portable. The libraries starting with javax.persistence are associated with JPA. Providers like hibernate provide non-portable properties. Thus best practice is to use javax.persistence whenever possible.

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

JPA is an abstraction over JDBC. JPA is a Specification. It Needs a provider. Hibernate is one of the many providers.

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

Hibernate = ORM + Implementaion of JPA Thus you will find some extra features in Hibernate.

-Any other differences between both?

Hibernate changed a lot in past few years. I find JPA to be consistent. So, if not needed, try to use javax.persistence libraries whenever possible.