Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Native vs Hibernate JPA

Hibernate website says there is a native Hibernate API as well as an implementation of JPA. What is the difference between the Native API and JPA implementation? Advantages, disadvantages?

I am working on a Spring MVC application, using Tomcat as the container, and MySQL for persistence. I've used Doctrine and Entity for PHP and .NET respectively in the past, using the code first approach. I would like to have something similar with Java. I'm newer to Spring and never used Hibernate. My team would like to use an ORM and Hibernate seems to be the most popular. We're not sure how Hibernate is going to workout or whether we should use native or JPA api. The application will be data driven, data entry, reporting, etc.

I've read that using JPA makes its easier to switch to another JPA implementation, although I don't know if that will be needed or not.

like image 964
greyfox Avatar asked Dec 29 '13 00:12

greyfox


People also ask

What's the difference between JPA and Hibernate?

JPA is the Java specification and not the implementation. Hibernate is an implementation of JPA and uses common standards of Java Persistence API. It is the standard API that allows developers to perform database operations smoothly. It is used to map Java data types with database tables and SQL data types.

What is the advantage of JPA over Hibernate?

JPA is a standard, while Hibernate is not. In hibernate, we use Session for handling the persistence of data, while in JPA, we use Entity Manager. The query language in Hibernate is Hibernate Query language, while in JPA, the query language is Java Persistence query language. Hibernate is one of the most JPA providers.

Can we use Hibernate without JPA?

You don't need to include JPA separately. It's included in Java EE (well, Hibernate libs also need to have copies of the entities since it implements them).


1 Answers

JPA is a standard for accessing relational databases through an object oriented API. Hibernate is an implementation of this API. When you want to use JPA you need some vendor to implement it, Hibernate is a good choice but there are others like EclipseLink.

Hibernate exists longer than JPA. The native, older API (which was a model for JPA) still exists, and sometimes it offers more possibilities than are exposed through JPA (e.g. orphan removal). If you need those you need to use the native API. Using JPA has other merits, most important (in my opinion) more developers that know it. And you still can use some Hibernate specifics through configuration.

In general IMO it pays to use the standard API, here JPA, and do an experienced decision on the implementation to use.

Most tutorials that use Hibernate natively are quite old - as is Hibernate 3, a pre-JPA release. While there are good reasons to use it, they (IMO) typically don't apply to the general audience. So if you are just beginning to learn in this field I would suggest to start with JPA.

As for recommendations on offsite resources: For good reasons they are not on topic here. But the current official Hibernate documentation would be a good start, as would be to look for toturial for at least Hibernate 4.

like image 141
Hauke Ingmar Schmidt Avatar answered Sep 19 '22 12:09

Hauke Ingmar Schmidt