Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 981
Ali Bassam Avatar asked Mar 09 '14 18:03

Ali Bassam


People also ask

Is javax persistence same as hibernate?

Hibernate is an implementation of the Java Persistence API, and where possible, you should use the standard annotations (in javax. persistence). This way, you could theoretically run your code on other JPA implementations. Only when you need Hibernate-specific functionality should you use the Hibernate annotations.

What is the main difference between JPA and Hibernate?

JPA is responsible for managing relational databases in Java applications. Hibernate is an ORM tool used for saving the state of the Java object in the database. It is defined under the javax. persistence package.

What is difference between Hibernate and Spring data JPA?

JPA uses EntityManager interface to create/read/delete operation and maintains the persistence context. Hibernate uses Session interface to create/read/delete operation and maintains the persistence context. JPA uses JPQL (Java Persistence Query Language) as Object Oriented Query language for database operations.

What is javax persistence used for?

Java Persistence is the API for the management for persistence and object/relational mapping. Used with the Access annotation to specify an access type to be applied to an entity class, mapped superclass, or embeddable class, or to a specific attribute of such a class. Represents an attribute node of an entity graph.


3 Answers

You use javax.persistence to keep implementation-agnostic (i.e. 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.

like image 61
djb Avatar answered Nov 09 '22 00:11

djb


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.

like image 39
scubadev Avatar answered Nov 09 '22 02:11

scubadev


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.

like image 3
Karthik Prasad Avatar answered Nov 09 '22 02:11

Karthik Prasad