Deep Dive on Spring Boot and JPA Implementation: A to Z

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

Photo by Tracy Adams on Unsplash
  1. Save, update and delete object into the table
  2. Retrieve data from a table using JPA default way, projection, and custom query
  3. Integrate pagination on JPA

Setup of JPA & H2 Database

Here, we will use H2 database because this is a Very fast, open-source, in-memory database. H2 is a relational database management system written in Java. It can be embedded in Java applications or run in client-server mode. No external database required to setup. For the production environment, you can add other databases that you required.

Entity design and save, update and delete

For our example purpose, we will create a table named “customer” and the Entity class name is “Customer”. This entity is given bellow

Save/Update Customer in table

For savings data into a table, you have to just build an object and call the repository save function. Here is the code:

(id, first_name, gender, last_name, mobile_number, present_address, permanent_address)
(null, ?, ?, ?, ?, ?, ?)

Fetch Data based on mobileNumber

Here, I am searching for customers based on mobileNumber. JPA query is
List<Customer> findAllByMobileNumber(String mobileNumber);
After execution of this code the actual query is like below:

2021–03–24 00:31:49.104 INFO 10713 — — [nio-8081-exec-6] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
select as id1_0_,
customer0_.first_name as first_na2_0_,
customer0_.gender as gender3_0_,
customer0_.last_name as last_nam4_0_,
customer0_.mobile_number as mobile_n5_0_,
customer0_.present_address as present_6_0_,
customer0_.permanent_address as permanen7_0_
customer customer0_

Fetch Specific column from the table

In the upper example, it is selecting all columns from the table. What if we want just some specific field like ID, firstName, lastName, and mobileNumber only. How can get this? We can achieve this in bellow way:

Now the actual query will be
select as col_0_0_,
customer0_.first_name as col_1_0_,
customer0_.last_name as col_2_0_,
customer0_.mobile_number as col_3_0_
customer customer0_

Pagination based query

In Spring data JPA we can easily integrate pagination. The Pageable Interface has the below properties :

Back-end developer, Spring, JAVA, Distributed system, AWS, Google Cloud, Docker, CD/CI, Kubernetes, databases.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store