What are the differences between Hibernate and JPA?

北慕城南 提交于 2019-12-30 04:11:27

问题


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 switching to Java EE 6 (Im in love :) ), I use JPA for my ORM needs.

It is being a few months since i use it, but i dont 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 dissadvantages 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?


回答1:


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.




回答2:


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.




回答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.




回答4:


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.




回答5:


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.




回答6:


interface JPA {
    void merge(Object o);
}

class Hibernate implements JPA {
    @Override
    public void merge(Object o) {
        // Implementation here
    }
}


来源:https://stackoverflow.com/questions/5678883/what-are-the-differences-between-hibernate-and-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!