Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @Entity in Hibernate and JPA

When i am doing sample Hibernate standalone program, little bit confusing was created in my mind with the usage of @Entity annotation.

Here my question is, I have one persisted class with @Entity from javax.persistence package then it's working fine but when i replaced @Entity annotation with Hibernate API(i.e from org.hibernate.annotations package) then it's giving org.hibernate.MappingException: Unknown entity: com.jetti.test.Employee

Give some more explanation are highly appreciated.

like image 890
jettisamba Avatar asked Oct 28 '14 10:10

jettisamba


3 Answers

@javax.persistence.Entity is still mandatory, @org.hibernate.annotations.Entity is not a replacement.

Documentation

So, hibernate's @Entity just complements the javax.persistence.Entity, and gives a few more fine tuning options.

like image 175
Predrag Maric Avatar answered Nov 11 '22 09:11

Predrag Maric


The difference is the same between Hibernate and JPA, rather JPA is just a specification, meaning there is no implementation, and Hibernate is an implementation.

You can annotate your classes with JPA annotations, but without an implementation nothing will happen.

In an abstract way you can consider "JPA" as the guidelines.

When you use Hibernate with JPA you are actually using the Hibernate by JPA implementation. The benefit is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification (Eclipse Link, DataNucleuse,..) else if you use directly Hibernate you cannot just switch over to another ORM.

I hope that it was helpful.


JPA is not an ORM implementation but is just guidelines to implement the Object Relational Mapping (ORM) and there is no underlying code for the implementation. it will not provide any concrete functionality to your application. Its purpose is to provide a set of rules and guidelines that can be followed by JPA implementation vendors to create an ORM implementation in a standardized manner.

Hibernate is a JPA provider. When there is new changes to the specification, hibernate would release its updated implementation for the JPA specification. Other popular JPA providers are Eclipse Link (Reference Implementation), OpenJPA, etc. See Other provider here

@javax.persistence.Entity is a GuideLine for a provider that implements JPA guideline

@org.hibernate.annotations.Entity is an implementation for a Hibernate ORM

Futhermore you can see this other topic

like image 34
Xstian Avatar answered Nov 11 '22 08:11

Xstian


The org.hibernate.annotations.Entity annotation is deprecated and it is scheduled for deletion:

@deprecated See individual attributes for intended replacements. To be removed in 4.1

Every attribute has a dedicated annotation (e.g. DynamicInsert, DynamicUpdate) and so you should always sue the javax.persistence.Entity alternative.

like image 3
Vlad Mihalcea Avatar answered Nov 11 '22 08:11

Vlad Mihalcea