Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JPA Entity and Hibernate Entity

When I annotate a class with @Entity and try to resolve the dependencies, I get to choose the package between two different packages, javax.persistence.Entity and org.hibernate.annotations.Entity

The javax package is JPA's entity-annotation, but why is there a hibernate entity-annotation and difference does it have with JPA's annotation? Is it just an extension to allow more attributes to be defined?

like image 579
Kim L Avatar asked Jun 05 '09 11:06

Kim L


People also ask

What is the difference between Hibernate and Spring data JPA?

What Is the Difference Between Hibernate and Spring Data JPA? Hibernate is a JPA implementation, while Spring Data JPA is a JPA Data Access Abstraction. Spring Data offers a solution to GenericDao custom implementations. It can also generate JPA queries on your behalf through method name conventions.

What is a JPA entity?

Using JPA, you can designate any POJO class as a JPA entity–a Java object whose nontransient fields should be persisted to a relational database using the services of an entity manager obtained from a JPA persistence provider (either within a Java EE EJB container or outside of an EJB container in a Java SE application ...

What is difference between EntityManagerFactory and SessionFactory?

Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will require extra efforts.


2 Answers

org.hibernate.annotations.Entity has some extra attributes that javax.persistence.Entity has not standarized. The extra features will only work if using hibernate's AnnotationConfiguration directly or if hibernate is the JPA provider.

from the FAQ: edit: new link the specific question: edit: new link the answer:

I use @org.hibernate.annotations.Entity and get an Unknown entity exception

Always import @javax.persistence.Entity

@org.hibernate.annotations.Entity completes @javax.persistence.Entity but is not a replacement

For instance, there is an attribute called optimisticLock, which tells hibernate whether to use the standard version column or to compare all columns when updating. This behavior is not in the JPA spec, so in order to configure it, you must use hibernate specific extension found in their own annotation.

Like this:

@Entity @org.hibernate.annotations.Entity(optimisticLock=OptimisticLockType.ALL) public class MyEntity implements Serializable { ... } 
like image 130
HMM Avatar answered Sep 23 '22 06:09

HMM


@org.hibernate.annotations used in your project, if suppose you want to use JDBC template or ibatis we need to change the code. if we use javax.persistence there is no need to change the code. This is the main difference between org.hibernate.annotations and javax persistence

like image 21
Ganesamoorthi Avatar answered Sep 21 '22 06:09

Ganesamoorthi