How to print a query string with parameter values when using Hibernate
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When using Hibernate in Java applications to handle database operations, developers often need to debug issues related to SQL statements generated by Hibernate, particularly the exact query strings populated with parameter values. Hibernate, by design, does not log the SQL statements with parameter values directly due to potential security and performance reasons. However, understanding how to print out these SQL queries with their parameter values can be crucial for debugging and optimizing database interactions. This discussion provides a comprehensive guide on achieving this, covering various techniques and tools.
Enabling Hibernate SQL Logging
The starting point to view SQL queries in Hibernate is to enable the logging of SQL statements. Hibernate provides several settings to manage this:
- Show SQL: Set
hibernate.show_sqltotruein your Hibernate configuration file or as a Java system property. This will print the SQL statements to the console or log, but it will not include parameter values (they appear as?).
- Format SQL: For better readability, set
hibernate.format_sqltotrue. This is especially useful when dealing with complex queries:
However, to actually see the parameter values, additional configuration is necessary.
Using a Logging Framework
To view the parameter values, you can integrate a logging framework that supports JDBC logging. One such popular framework is Log4jdbc-Log4j2, which intercepts JDBC calls and logs them, complete with actual parameter substitutions.
To implement this:
- Include the log4jdbc-log4j2 dependency in your project.
- Replace your standard JDBC driver in the Hibernate configuration with the
net.sf.log4jdbc.DriverSpyclass. - Finally, configure your logging framework (like log4j or log4j2) to monitor
jdbc.sqlonly,jdbc.sqltiming,jdbc.audit, andjdbc.resultsettable.
Example Hibernate configuration using Log4jdbc-Log4j2:
Using Hibernate Built-in Statistics
Another approach is to use Hibernate's built-in statistics mechanism to gather SQL details:
- Enable the generation of statistics by setting
hibernate.generate_statisticstotrue. - Access these statistics programmatically through the
SessionFactoryor by logging.
However, note that this method still won’t show individual parameter values directly on log files.
Analyzing Hibernate Query Details
For more in-depth analysis, consider using Hibernate’s Interceptor interface. You can implement methods like onPrepareStatement to log the final version of every SQL statement right before execution, along with its parameter values.
Precautions
While logging SQL queries and parameters is invaluable for debugging, remember to handle sensitive data carefully. Ensure that logs do not store sensitive information like passwords or personal identifiers especially in production environments.
Summary Table
| Feature | Property | Description |
| Show SQL | hibernate.show_sql | Displays SQL in the console/log; does not include parameters. |
| Format SQL | hibernate.format_sql | Formats the printed SQL query for readability. |
| JDBC Logging | Log4jdbc-Log4j2 | Uses driver DriverSpy to log detailed queries including parameters. |
| Hibernate Statistics | hibernate.generate_statistics | Provides statistical data but not detailed parameter values. |
| SQL Interception | Hibernate Interceptor | Allows detailed monitoring and can log exact parameter values. |
Conclusion
Printing out query strings complete with parameter values when using Hibernate requires a combination of Hibernate settings, logger configurations, and third-party tools. Each approach has its nuances and might be more appropriate in different scenarios. It's crucial to balance between detailed logging for debugging and the overhead introduced by logging, not mentioning the security aspects of exposed data.

