paint-brush
The Difference Between JDBC, JPA, Hibernate, and Spring Data JPAby@staceybdavis
38,371 reads
38,371 reads

The Difference Between JDBC, JPA, Hibernate, and Spring Data JPA

by Stacey DavisAugust 25th, 2021
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Java Database Connectivity (JDBC) is the bridge between the Java world and the database world. Spring Data JPA is a library that adds an extra layer of abstraction on top of the ORM JPA implementation. Hibernate is the most popular ORM (Object Relational Mapping) framework for working with a database, I leave it as my choice. The only thing you need to specify is the host for your database, and password password to access it to the database.

Company Mentioned

Mention Thumbnail
featured image - The Difference Between JDBC, JPA, Hibernate, and Spring Data JPA
Stacey Davis HackerNoon profile picture


Most Java programs use relational databases (MySQL, PostgreSQL) to store data for further processing. This is very common, especially when it comes to web applications that have a very wide range of user interactions.


Connecting a database to a Java application is not an easy process. You need to consider the connection pool, the data access layer, the correct mapping of the database table and Java object.


Fortunately, there are already serious developments in the form of libraries and frameworks that programmers can use to make it easier for them to work with the above tasks.


In this article, I will try to reveal only the difference between JDBC, JPA, Hibernate, Spring Data JPA in the framework of executing queries through Java, without considering connection pools and other nuances.


Java Database Connectivity - JDBC


The very first way to connect a Java application to a database is using JDBC (Java Database Connectivity).


Sun Microsystems released JDBC as part of the JDK in February 1997. We can say that JDBC is the bridge between the Java world and the database world. After all, the first thing we look for when we want to connect a database to our application is the JDBC driver.


If we are working with Maven, then we are looking for a driver dependency for a specific base. One of the disadvantages of JDBC is that the code that comes out at the end looks very large in volume (although it does not do much work).


The process is complicated when the Java object that we are trying to save in the database or retrieve is very large. You need to properly map database fields and Java class fields.


It happens that in the process of work you need to add a field to the finished table in the database. Then, you need to find all requests to the database in the application code to add this field.


To get the most out of the "pain" of working with JDBC, you need to work with it. And for those who are already familiar with this functionality, I think explanations will be superfluous.

The Java Persistence - API

JPA aims to address the above deficiencies. The Java Persistence API specification is a technology that allows you to conveniently map a Java object and a database table.


In JDBC, when you write each query, you need to include in the code all the details required for CRUD operations, such as table names, column names. In JPA (which uses JDBC "under the hood"), you also specify this data, but only once, when annotating a Java class.


The JPA specification itself is not a tool or framework; rather, it defines a set of concepts that can and should be implemented by any other tool.


Since JPA is just a specification, you need a tool to implement it.


Since Hibernate is the most popular ORM (Object Relational Mapping) framework for working with a database, I leave it as my choice. But the same goes for other ORM libraries that you probably haven't even heard of.


You can think of JPA as an interface and Hibernate as an implementation. Without Hibernate, JPA will be of little use to your code. Although both Hibernate and JPA can be used separately in conjunction with other tools.


What about Spring Data JPA?


This library is part of the Spring Framework, one of the most popular Java frameworks today. Spring Data's goal is to reduce the amount of boilerplate required to implement data access layers for various databases.


Spring Data JPA is a library that adds an extra layer of abstraction on top of the ORM JPA implementation. By default, Spring Data JPA uses Hibernate as the ORM provider (to execute queries). This, by the way, can be changed using Spring settings. Although I would not recommend doing this to inexperienced users.


If you are using Spring Boot with Spring Data JPA, then you have all the necessary settings for connecting a Java application to the database. The only thing you need to specify is the host for your database, username, and password to access it.


Spring Boot provides auto-configuration for all database connections. Including the connection pool. To learn more about the processes of Spring Data JPA - you should read Spring interview questions. It will be helpful for newbies as for pro-coders.


As you can see, over time, JDBC, which was released in 1997, has become overgrown with layers of abstractions that allow programmers to focus on solving a business problem instead of wasting their time writing a bunch of configuration classes for setting up a database.


Now you have the opportunity to connect any data store (not just a relational database) in a couple of clicks, without having to turn on "Dancing with a tambourine", as was done before. SQL queries will have to go through many layers before reaching the base itself. But from my own experience, I can say that in most cases, the drop in query execution speed is not observed if you use JDBC or Spring Data JPA. You just need to be careful with complex and large requests.


For newbies, I recommend trying JDBC first. Do this so you can compare them with Hibernate and Spring. The difference will be really noticeable. In doing so, you will discover how all frameworks work under the hood.