The relationship between Hibernate and JPA is finally understood

Source: Internet
Author: User

I know that JPA is a specification, and hibernate is an implementation of it. In addition to hibernate, there are Eclipselink (formerly TopLink), OPENJPA, etc. to choose from, so one advantage of using JPA is that you can replace the implementation without having to change too much code.

When you define the model in play, you use the JPA annotations, such as Javax.persistence.Entity, Table, Column, Onetomany, and so on. But they provide the functional basis, sometimes want to define the finer, will inevitably use hibernate itself annotation. I thought that JPA was so weak to use it for, why not use hibernate directly? Anyway, I'm not going to replace it with another implementation.

Because I quickly decided not to use Hibernate, this problem has been put down. Until I'm in the new company, I'm going to use hibernate for the project.

I wanted to put aside JPA and use hibernate annotations directly to define the model, and soon found several problems:

    1. There are entity in JPA, but there are table,hibernate, but the content is different.
    2. There are column,onetomany in JPA, not in Hibernate, no substitutes.

I thought hibernate's support for JPA was another set of annotations that were dedicated to JPA, but it doesn't seem to be. Some important annotations such as column, Onetomany, and Hibernate are not available, which means that the JPA annotations are already at the heart of Hibernate, and Hibernate provides only a few additions, not two sets of annotations. If so, hibernate's support for JPA is really sufficient, and we will use the Hibernate annotations to make sure that JPA is used.

Is that the situation? In the group (Scala Communication group 132569382) Friends despise but did not give a satisfactory answer, I think of the Almighty StackOverflow, up to mention two asked:

    1. Http://stackoverflow.com/questions/8306742/if-i-want-to-use-hibernate-with-annotation-do-i-have-to-use-javax-persistence
    2. Http://stackoverflow.com/questions/8306793/why-jpa-and-hibernate-both-have-entity-and-table-annotations

The first one is asking if you want to use Hibernate to annotate, is it sure to use JPA. Netizen's answer: "Yes." If hibernate thinks the JPA annotations are sufficient, it is used directly. Or you'll get a self-made supplement. "

The second question is, JPA and hibernate provide the entity, which one should we use, or can we use two together? The netizen replied that "Hibernate's entity is inherited from JPA, so if you feel that JPA is not enough, use hibernate directly."

The relationship between Hibernate and JPA is finally understood

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.