Getting started with Spring Boot Data JPA

Mahesh Bonagiri
3 min readJul 13, 2021

Part II (Pagination & Sorting)| Part III (Auditing) |Part IV (Projections)

In Previous post I explained how to get started with Spring boot. In this post we will use Spring Boot along with Spring Data.

Spring Data JPA provides complete abstraction over the DAO layer. We don’t need to write the implementation for the DAO layer anymore. Spring Data JPA handles most of the complexity and reduces the boilerplate code required by JPA.

Let’s start:

we will use Spring Initializer generate the base for our project, in my previous post I have explained how to get starting with Spring Boot + Eclipse.

Click ADD DEPENDENCIES and select Spring Data JPA, Lombok and then H2 Database. Generate and import into your favorite IDE. Eclipse will automatically download all required dependencies once imported.

The generated pom.xml file should look like below.

Define a Simple Entity

Lets create an Entity called BookEntity as shown below.

Here we have a BookEntity class with four attributes id, name, author and price. The constructor is used to create instances of Book Entities to be saved to the database. The class is annotated with @Entity, indicating that it is a JPA entity, @Getter/@Setter (Lombok annotations) removes boilerplate code, @ToString method print outs the object’s properties

BookEntity object’s id property is annotated with @Id so that JPA recognizes it as the object’s ID. and @GeneratedValue indicates that the ID should be generated automatically.

Repository Interface:

The Repository represents the DAO layer, which typically does all the database operations. Thanks to Spring Data, who provides the implementations for these methods.

Let’s have a look at our BookRepoisitory, which extends the CrudRepository, By extending CrudRepository, BookRepository inherits several methods for working with BookEntity persistence, including methods for saving, deleting, and finding Book entities.

Spring Data JPA also lets you define other query methods by declaring their method signature. For example, BookRepository includes the findByAuthor() method.

Create an Application Class

Create SpringbootDataJpaIntroApplication class as shown below. Using CommandLineRunner we will first save few records and then later fetch those records.

When you run your application, you should see output similar to the following:

Adding show_sql will display generated SQL statements and format_sql will format those.

spring.jpa.show-sql=true

spring.jpa.properties.hibernate.format_sql=true

Sample log statements after adding show-sql and format_sql properties

Bonus Point:

Hibernate has basic logging feature to display the SQL generated statement with “show_sql” configuration property, However, it just isn’t enough for debugging, the Hibernate SQL parameter values are missing. we can configure logging to also output the bind parameters as below.

just add below two configuration properties in application.properties

logging.level.org.hibernate.SQL=debug

logging.level.org.hibernate.type.descriptor.sql=trace

it will output below log.

Conclusion:

If you would like to refer to the full code, do check https://github.com/projectk-user1/Springboot-learning.git

Please do refer my other articles on Spring boot.

Feel free to provide your suggestions in comments section

--

--