JDBC or ( JPA also Hibernate) in Spring Framework

Let’s dive deep to settle on a framework for Database operations DDL

JDBC

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language that defines how a client can access a database.

In February 1997, Sun Microsystems released JDBC as a part of the JDK. Programmers can use these standard interfaces and classes to write applications that connect with databases, send SQL queries, and process the results.

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.

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

Pros of JDBC

  • Simple SQL processing

  • Good performance with large data

  • Very good for applications where every zeptosecond counts

  • Simple syntax so easy to learn

  • It is useful in applications where full control of the execution is required.

Cons of JDBC

  • Large programming overhead

  • There is no encapsulation

  • Hard to implement the MVC concept

  • Queries are DBMS specific

JPA

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.

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.

JPA is a Java standard for binding Java objects to records in a relational database. We can say it's one of the possible solutions for ORM, with this developers can perform CRUD operations on Relational Databases using Java Objects(POJO).

The first version of Java Persistence API, JPA 1.0 was released in 2006 as a part of the EJB 3.0 specification with the motive to reduce the boilerplate code for Object Mapping and hide SQL from the developer.

Implementations of the JPA specification are available from many vendors, such as Hibernate, Toplink, iBatis, OpenJPA, etc. Under the hood, Hibernate and most other providers for JPA uses JDBC to read and write from and to the DB.

It's a tough decision for System Architect to settle on one to communicate with the Back-End database as JPA and JDBC have very different approaches for working with the persistence layer.

JPA and JDBC differ primarily in their level of abstraction. JDBC is a low-level standard for interaction with databases. JPA is a higher-level standard for the same purpose. With JDBC you will have to write all the SQL stuff, validations, ORM, transaction management. On the other hand, you can code directly with business logic using JPA as JPA will handle all the SQL stuff and ORM tasks.

Pros of JPA

  • Provides encapsulation which lets developers focus on business logic.

  • Integration with java beans validation for Easier Validations

  • Provides Database Independence

  • A single request manages all the SQL in a single transaction.

Cons of JPA

  • Not good for batch transactions. Consumes too much memory.

  • Not thread-safe.

  • A better understanding of what happens is required to specify additional fetches and improve performance.

  • JPA is 4x slower than JDBC when it comes to large-batch processing.

  • Mapping between database tables can be a bit challenging.

Performance comparison of Spring Data-JPA and Spring-Data-JDBC …

I created demo applications to evaluate and understand two modules provided by Spring Data Framework.

  1. Spring-Data-JDBC: https://github.com/redhabayuanggara21/S-JDBC-Demo

  2. Spring-Data-JPA: https://github.com/redhabayuanggara21/S-JPA-Demo

Did a performance analysis of JPA and JDBC modules by Spring-Data, where I found out that JDBC outperforms JPA by a significant factor in one-by-one inserts and by a margin in batch inserts.

Considering JPA’s offerings, this difference is considerable if implemented with the attention to the details of the flow & used according to the need.

graphical comparison for JPA and JDBC sequential inserts

JBDC is ~20% faster than JPA

graphical comparison for JPA and JDBC batch inserts

JDBC is ~4% faster than JPA

Records were inserted synchronously.

Batch size used in analysis is 100

The database used in the performance analysis is Microsoft Azure-SQL.

SQL table was created with auto-increment SQL generated id. (IDENTITY)

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.

Result

JPA’s ORM mapping is its major advantage over JDBC because it converts object-oriented Java code to the back-end database without needing time-consuming, error-prone coding.

But JDBC allows you to do more things with the Database directly & give more control.

There is a trade-off between coding time and fine-grained control.

Before finalizing a framework, one must engineer and understand the future requirements of the application and the Uses.