Fotolia

Tip

How JPA and Hibernate simplify data persistence

JPA is the Java standard for data persistence, especially for relational systems. Here, we explore how Hibernate and JPA work together to make database processes easier.

What's the difference between Hibernate vs JPA? That's a common question Java developers dabbling on the database side often ask, right up there with difference between Hibernate and JDBC, or how do JDBC and ODBC compare?

The very short answer is that JPA is a specification, whereas Hibernate is an implementation of that specification. So, do you want to use the Hibernate implementation of JPA? Let's dive into the details of both.

What is JPA?

In Java programming, JPA stands for Java Persistence API. JPA's main purpose is to provide classes and methods that enable a Java application to interact with a database over JDBC in order to store or retrieve persistent data. Adhering to the JPA specification is not the only way to read or write to a database in Java. There are object-relational mapping (ORM) frameworks for Java that do not follow the JPA specification, such as ActiveJDBC.

In addition, there are other ways to handle persistent data in Java without using an ORM framework at all, although these approaches are rare and not best practice in most cases.

Despite the availability of several other solutions, JPA is almost certainly the most widely used Java database storage specification, so it's familiar to most programmers, which is an advantage when you're writing code other people have to maintain or extend. The first complete JPA specification, JPA 1.0, was introduced in 2006. An updated version, JPA 2.0, debuted in 2009. The latest maintenance release is JPA 2.2, which appeared in June 2017.

What is Hibernate?

Hibernate ORM, popularly known as just Hibernate, is a framework that maps object-oriented domain models to databases in Java.

Hibernate was first released in 2001, back when data persistence architectures in Java applications were not as universally defined as they became when JPA came along several years later. However, modern versions of Hibernate are certified for compliance with the JPA specification.

Because JPA compliance in Hibernate is achieved with a wrapper, Hibernate ORM itself does not technically follow the JPA specification. Hibernate implementations, though, effectively adhere to JPA. From the developer's perspective, if you're using Hibernate, you're following the JPA specification.

Comparing Hibernate and JPA

Because Hibernate is an example of a JPA implementation, it doesn't really make sense to think of Hibernate and JPA as competitors. It's like saying Posix (Portable Operating System Interface) vs. Linux or containers vs. Docker. Java developers should scrap that debate and consider whether they should really use JPA at all and, if they do, whether they want to use Hibernate or another implementation.

JPA is not the only way to persist data in a Java application.

As noted above, JPA is not the only way to persist data in a Java application. Because Java has several implementations of the JPA specification, however, the main advantage of specification is that you can change your data persistence framework easily if you want by switching between different implementations, as long as they are all JPA-compliant.

In practice, if you switch from one JPA-compliant framework to another, it'll probably require some modifications to your code. Some of your methods within one implementation most likely won't be available in another one, and you'll have to update your code to address this.

In general, however, the move from one JPA-compliant framework to another is a lot easier than a switch between two entirely dissimilar ORM frameworks.

Beyond Hibernate and JPA: Other ORM frameworks

As we've already talked about, there is much more to the data persistence debate than just JDBC and Hibernate. JPA is not the only way to persist data in a Java application. Some Java developers contend that Java ORM frameworks not based on JPA are easier to learn and deliver better performance. If you agree, you might want to check out non-JPA ORM frameworks, such as ActiveJDBC.

Furthermore, Hibernate is not the only JPA-compliant Java ORM framework. Other JPA-compliant options include EclipseLink, DataNucleus and Apache OpenJPA. At a high level, all of these frameworks deliver the same functionality based on the same specification. But the methods each make available all differ. One JPA ORM may be a better fit than another, depending on exactly what you are trying to do or on personal preference.

The bottom line: You have lots of options for data persistence within Java applications. Several frameworks exist within the JPA category, and there are options beyond that, too.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close