Java.Hibernate.Middle.What’s the purpose of the hibernate.show_sql property?

Short Answer

The hibernate.show_sql property tells Hibernate to log all SQL statements it generates directly to the console (standard output).
It’s a helpful debugging tool to see the actual SQL queries Hibernate sends to the database.


🔎 Detailed Explanation

🔹 What happens when you enable it?
When you set:

hibernate.show_sql=true

Hibernate prints every SQL statement it executes, such as:

select user0_.id as id1_0_, user0_.name as name2_0_ from User user0_;

✅ This is incredibly useful for:

  • Understanding what queries Hibernate generates from your HQL or Criteria queries.
  • Troubleshooting N+1 problems or inefficient queries.
  • Verifying correct table/column names, joins, and conditions.

🔹 What does it log?

  • All SQL commands: SELECT, INSERT, UPDATE, DELETE, schema DDL (if enabled).
  • But it only shows the SQL text, not the parameter values used in prepared statements.

🔹 Limitations
❌ Doesn’t log parameter bindings → so you won’t see exact query values.
✅ For parameter values, you need:

  • hibernate.format_sql → makes SQL output more readable (pretty-printed).
  • A dedicated SQL logger like P6Spy, datasource-proxy, or Hibernate’s org.hibernate.type.descriptor.sql logging category.

🔹 Example configuration
In your application.properties or hibernate.cfg.xml:

hibernate.show_sql=true
hibernate.format_sql=true

🔹 Spring Boot note
Spring Boot passes these Hibernate properties to the JPA provider automatically if you include them in application.properties.


📌 Key Takeaways

hibernate.show_sql=true helps debug and analyze SQL generated by Hibernate.
✅ Great for development, but should be disabled in production → can log sensitive data and degrade performance.
✅ Combine with hibernate.format_sql=true for better readability.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.