Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate or JPA annotations to use

I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. For Annotations based configuration we have two options

  1. JPA based annotations using javax.persistence.*
  2. Use Hibernate Native Annotations org.hibernate.annotations.*

Currently we use JPA based annotation configuration for our POJO files and Hibernate native API like SessionFactory, Session, etc to open session and perform DB operations.

I have these questions:

  1. Is there any problem mixing both JPA annotations and use Hibernate native API?
  2. This link explains one such issue (cascade-jpa-hibernate-annotation-common-mistake)
  3. Please provide your expertise, which type of annotations to use
    1. JPA
    2. Hibernate native
    3. Mix both of them?
like image 913
Leo Prince Avatar asked Apr 17 '13 05:04

Leo Prince


People also ask

Should I use hibernate or JPA?

Hibernate is an implementation of JPA. Hence, the common standard which is given by JPA is followed by Hibernate. It is a standard API that permits to perform database operations. It is used in mapping Java data types with SQL data types and database tables.

Why we are using JPA annotation instead of hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.

Which annotation is used for JPA?

JPA annotations are used in mapping java objects to the database tables, columns etc. Hibernate is the most popular implement of JPA specification and provides some additional annotations. Today we will look into JPA annotations as well as Hibernate annotations with brief code snippets.

Is Spring data JPA better than hibernate?

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.


2 Answers

Hibernate provides one of the JPA implementations. If you use purely JPA in your code, you are free to change to a different implementation if a requirement arises. For example, EclipseLink/TopLink and OpenJPA are implementations which may be required for a different customer. A comprehensive list of implementations is here.

If you are compelled to use any exotic features provided by hibernate which are not in JPA specification, you should go for hibernate specific APIs in your code base. A related discussion from hibernate forum here.

like image 185
maggu Avatar answered Sep 22 '22 18:09

maggu


We mixed some of these annotations since with the version of hibernate we were working those days, some features were not available on the JPA spec at that time. For instance to store a boolean value as a 'Y' or 'N' on th DB you have hibernate types you can use. But no such feature was available with the JPA spec at that time. I do not know about the status now. Also for orphan removal also those days JPA did not have the feature, but now i belive they provide an attribute called orphanRemoval on your cascade options. Also you have certain features such as @BatchSize to increase fetching performance with respect to bags. I am unaware if such features are available on the JPA spec yet.

In my experience, mixing and matching plus points from both would be beneficial given that you have no need of switching from one ORM to another.

like image 39
dinukadev Avatar answered Sep 21 '22 18:09

dinukadev