DEV Community

Cover image for Hibernate - vs JDBC vs JPA vs Spring Data JPA
Yiğit Erkal
Yiğit Erkal

Posted on • Updated on

Hibernate - vs JDBC vs JPA vs Spring Data JPA

You have a java program on one side and the relational database on the other side. Let's check what happens when you connect these and get the overview of the tools which can you help to do this.

jdbc and java application sql

The first and the basic solution to connect java to the database is JDBC. JDBC stands for "Java Database Connectivity". It is a part of Java which was released by Sun Microsystems in 1997. JDBC makes it possible to do establish a connection with a data source, send queries and update statements, and process the results.

jdbc database connection sql

Simply, JDBC makes it possible to do the following things within a Java application:

  • Establish a connection with a data source

  • Send queries and update statements to the data source

  • Process the results

JDBC is a much better option, if:

  • If an app is using a simple database that does not require to migrate, or

  • If the application needs data to be stored in database tables that won’t require object-mapping to 2 or 2+ table versions.

In short, JDBC is a better option for simple processes. The only disadvantage with JDBC is that where you can often have some crappy code where lots of mapping between data sets and logic is mixed with SQL. So, that's when the JPAs come handy.

jpa database sql

It's a specification that allows you to map between objects in code and database tables. This can hide the SQL from the developers and let them deal with classes and objects only in order to perform CRUD operations. You need to expose all the details such as table like table name and column name while in JPA which is using JDBC underneath you can specify these details using Java annotations.

JPA is not a tool or framework. It defines a set concepts that can be implemented by another tool or framework.

That tool or framework can be Hibernate. I like the explanation "JPA is the dance, Hibernate is the dancer" so JPA is the interface and Hibernate is the implementation.

jpa database sql hibernate

  • Unlike JDBC, Hibernate connects with the database itself and uses HQL (Hibernate Query Language) to execute the queries, then maps the results to java objects. Moreover, Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. I suggest you to google "Hibernate Annotations" as well in order to understand the concept of the ORM (Object Relational Mapping).

Lastly, Spring Data JPA is a part of Spring Framework. The goal of Spring Data is to reduce the boilerplate code. It adds an extra layer of abstraction on the top of your JPA provider such as Hibernate. By default, Spring Data uses Hibernate as ORM provider. It can be changed if you want to. If you use Spring with Spring Data, Spring Boot enables auto configuration for you to automatically connect. Only host, password and username is required in order establish a connection between database and your program. How simple is it, right?

spring jpa hibernate sql

Just create an interface which extends JpaRepository and you will just have all the CRUD methods like save(), update(), delete() and select() out of the box. Additionally, you can create your own operations with these methods. All the implementations are just made for you by Spring Data.

public interface IUserDAO extends JpaRepository<User, Long> {
    User findByName(String name);
Enter fullscreen mode Exit fullscreen mode

Final words... 🏁

The fastest approach is JDBC since it is the lowest level of java program and relational database. It doesn't have any levels on top of it so if you use it will be fast. However, it just has some complicity and it just adds some a lot of boilerplate code.

Also you can use a JPA and Hibernate to reduce the amount of boilerplate code and to enable mapping to show the mapping between your java program and database table.

Finally, Spring Data is the top level and you can use it just to avoid boilerplate code from Hibernate just to hide all the implementations working with database and just to focus on the level of tasks for business instead of thinking how to deal with the database.

Top comments (0)