简体   繁体   中英

Difference between Javax.Persistence and Hibernate.Annotations

I was reading the hibernate tutorial on tutorialspoint , they say that in order to use annotations with Hibernate it is required to have the following dependencies:

hibernate-annotations.jar
hibernate-comons-annotations.jar 
ejb3-persistence.jar

I've fetched those dependencies with Maven, but then when they continue with their example, they import javax.persistence rather than org.hibernate.annotations to use the annotations such as @Entity @Table etc...

What is the difference between javax.persistence and org.hibernate.annotations ? and why do I feel like I downloaded hibernate's annotations and I'm not using them?

You use javax.persistence to keep implementation-agnostic (ie you can change to use a different JPA implementation later.) If you want to just go with Hibernate, you can use org.hibernate annotations. But it's advisable to just import the javax classes rather. Then Hibernate will provide the implementation, but can be replaced by just switching in a different dependency.

The javax.* packages are defined by JSRs (java specification requests). Like an interface, the javax packages defines contracts. Providers, such as hibernate, supply the implementation. By using the javax imports, you decouple yourself from a specific provider and retain the ability to switch to a different implementation in the future.

Here's a list of other JSRs beyond javax.persistence ( https://jcp.org/ja/jsr/all ). The javax.persistence (2.0) specification is defined by JSR-317 .

javax.persistence is Java EE standard which is standard implementation. But org.hibernate.annotations hibernate specific implementation like wise there are other ORM framework implementation such as Toplink, Eclipselink etc. Tomorrow if you feel to switch the framework it becomes easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM