Relation between JPA and Hibernate annotation

后端 未结 3 1671
庸人自扰
庸人自扰 2020-12-15 00:29

I am quite new in Spring world and I am studying how to integrate Hibernate and Spring framework

i have some dount about the relation beetween Hibernate annotation a

相关标签:
3条回答
  • 2020-12-15 00:41

    Hibernate is an implementation of the JPA specification. JPA is just a specification, and a set of annotations and interfaces. You need an implementation of JPA to use it, and Hibernate is one of them. Just like to use JDBC, you need a database driver.

    The package of the annotation is javax.persistence, so they're JPA annotations. Hibernate annotations are in the package org.hibernate.xxx.

    0 讨论(0)
  • 2020-12-15 00:50

    Like @SJuan76 is saying, JPA (Java Persistence API) is a specification and Hibernate is one implementation. I agree with the others; stick to using plain JPA as much as you can. The JPA specification doesn't change very often, and when it does it is usually backward compatible. This means that your code will be protected from changes in the implementation. You are for example free to upgrade your Hibernate dependencies if your code don't depend on anything from org.hibernate.* but rather javax.persistence.*.

    Btw. JPA is in version 2.2 as of Hibernate 5.3. Read more about JPA here

    0 讨论(0)
  • 2020-12-15 00:53

    The answer is mixed.

    As others have pointed out, JPA is an specification an Hibernate provides an implementation. You use JPA annotations/API and, by including Hibernate jars in your classpath, Hibernate will provide the actual logic.

    Additionally, Hibernate offers an API that is unrelated to JPA. You can use that as well; the main difference is:

    • if you use the JPA API, you may later replace Hibernate by other JPA implementation (v.g. EclipseLink) and you will need no changes to your program

    • if you use directly Hibernate API, you have no implementation alternatives. The advantage may be that you can use features that Hibernate has defined but that are not part of JPA standard, which might be usefult to you.

    At any rate, what you should completely avoid is mixing JPA with the Hibernate API. As a beginner, I would advice you to stick to JPA.

    To ensure that you are using JPA, only include classes/annotations from java.persistence. Do not include anything from org.hibernate (or, if you want just to use Hibernate, do just the opposite).

    0 讨论(0)
提交回复
热议问题