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?
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.
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 ...
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.
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 { ... }
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With