How to set hibernate.format_sql in spring-boot?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Spring Boot applications, Hibernate is often used as the default JPA implementation for data persistence. One of the useful features provided by Hibernate is the ability to format SQL queries for better readability during development and debugging. The hibernate.format_sql property is an essential aspect of this functionality. Let's explore how to set this property, alongside other relevant considerations when using it in a Spring Boot application.
Setting hibernate.format_sql in Spring Boot
To enable SQL formatting, you need to modify your Spring Boot application's configuration settings. This can be done in two primary ways: using application.properties or application.yml files.
Using application.properties
Add the following line to your src/main/resources/application.properties file:
Using application.yml
Alternatively, if you're using YAML for configuration, you can set the property in the application.yml file like this:
By setting hibernate.format_sql to true, Hibernate will produce log output with prettified SQL, making it easier to understand and debug.
Understanding hibernate.format_sql
The hibernate.format_sql property is a Hibernate-native setting that instructs Hibernate to format the SQL queries it generates. By default, this is set to false in applications, meaning the output SQL will be a single, long line that can be hard to follow.
When hibernate.format_sql is set to true, Hibernate adds whitespace and line breaks to make SQL statements more readable. While this feature is handy during development, it might slightly affect performance, as the formatted SQL is more verbose.
Example
Consider the following simple entity:
With hibernate.format_sql=true, when you retrieve Employee entities, the log output will look something like:
Without SQL formatting, the output would be a single-line query:
Additional Considerations
Performance Implications
While the formatting feature is often negligible in its performance impact for most applications, consider turning it off in a production environment, where clarity of SQL statements is less critical than performance.
Related Properties
Here are a few related Hibernate properties that enhance its SQL logging capabilities:
hibernate.show_sql: Displays the generated SQL queries directly in the console. Use this for quick debugging but be cautious in production environments.application.propertiesexample:
hibernate.use_sql_comments: Appends comments to the SQL logs explaining which entity or operation triggered the SQL statement.application.ymlexample:
Configuration Summary
Below is a table summarizing the key Hibernate SQL properties and their usage:
| Property | Description | Default | Recommended for Dev | Recommended for Prod |
hibernate.format_sql | Formats SQL for readability | false | true | false |
hibernate.show_sql | Prints SQL to the console | false | true | false |
hibernate.use_sql_comments | Adds comments to SQL output | false | true | false |
Conclusion
The hibernate.format_sql property is a helpful configuration for developers who want to monitor and debug SQL queries generated by Hibernate. While it makes your development life easier, remember to assess the impact on performance, and likely disable it in a production setup.
By carefully considering and configuring these Hibernate SQL properties, you'll be better equipped to ensure your Spring Boot application is both developer-friendly and performance-optimized.
Related reading
- How to set initial value and auto increment in MySQL?
- How to set up a new SQL Server database to allow for possible replication in the future?
- how to set up an horizantal Partiting Distributed database Enviroment between two servers
- How to setup heterogeneous replication with tungsten?
- How to set HttpResponse timeout for Android in Java
- How to set HttpResponse timeout for Android in Java
- How to setup multiple connection pools when multiple datasources are used in Spring Boot?
- How to shard across a cluster of nodes based on the value of String?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.