Show Hibernate/JPA SQL Statements from Spring Boot

1. Overview

Spring JDBC and JPA provide abstractions over native JDBC APIs, allowing developers to do away with native SQL queries. However, we often need to see those auto-generated SQL queries and the order in which they were executed for debugging purposes.

In this quick tutorial, we’re going to look at different ways of logging these SQL queries in Spring Boot.

2. Logging JPA Queries

2.1. To Standard Output

The simplest way to dump the queries to standard out is to add the following to application.properties:

spring.jpa.show-sql=true

To beautify or pretty print the SQL, we can add:

spring.jpa.properties.hibernate.format_sql=true

While this is extremely simple, it’s not recommended, as it directly unloads everything to standard output without any optimizations of a logging framework.

Moreover, it doesn’t log the parameters of prepared statements.

2.2. Via Loggers

Now let’s see how we can log the SQL statements by configuring loggers in the properties file:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

The first line logs the SQL queries, and the second statement logs the prepared statement parameters.

The pretty print property will work in this configuration as well.

By setting these properties, logs will be sent to the configured appender. By default, Spring Boot uses logback with a standard out appender.

3. Logging JdbcTemplate Queries

To configure statement logging when using JdbcTemplate, we need the following properties:

logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

Similar to the JPA logging configuration, the first line is for logging statements and the second is to log parameters of prepared statements.

4. How Does It Work?

The Spring/Hibernate classes, which generate SQL statements and set the parameters, already contain the code for logging them.

However, the level of those log statements is set to DEBUG and TRACE respectively, which is lower than the default level in Spring Boot — INFO.

By adding these properties, we are just setting those loggers to the required level.

5. Conclusion

In this short article, we’ve looked at the ways to log SQL queries in Spring Boot.

If we choose to configure multiple appenders, we can also separate SQL statements and other log statements into different log files to keep things clean.

Related posts:

Spring REST API with Protocol Buffers
Spring WebClient Requests with Parameters
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Create the Prufer Code for a Tree
Collect a Java Stream to an Immutable Collection
Introduction to Spring Boot CLI
Difference Between Wait and Sleep in Java
Java Program to Perform Insertion in a BST
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Hash Tables Chaining with List Heads
Java Program to Implement Max Heap
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Find All Pairs Shortest Path
Introduction to Using FreeMarker in Spring MVC
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Implement Hash Tables
Spring Boot - Creating Docker Image
Spring’s RequestBody and ResponseBody Annotations
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
ExecutorService – Waiting for Threads to Finish
Spring Boot - Apache Kafka
Java Program to Implement the RSA Algorithm
Extract network card address
Read an Outlook MSG file
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Find the GCD and LCM of two Numbers
Java CyclicBarrier vs CountDownLatch
Quick Guide to java.lang.System
Java Program to Convert a Decimal Number to Binary Number using Stacks
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Hướng dẫn Java Design Pattern – Chain of Responsibility