Complete Guide to Printing SQL Queries with Parameter Values in Hibernate

Nov 11, 2025 · Programming · 22 views · 7.8

Keywords: Hibernate | SQL Logging | Parameter Binding | Log4j Configuration | P6Spy | JDBC Monitoring

Abstract: This article provides a comprehensive exploration of methods to print SQL queries with actual parameter values in Hibernate. It begins with the core approach of configuring loggers org.hibernate.SQL and org.hibernate.type to display SQL statements and bound parameters, including Log4j configuration examples. The limitations of the traditional hibernate.show_sql property are analyzed. The article then discusses the verbose nature of log output and presents alternative solutions using JDBC proxy drivers like P6Spy. Through code examples and configuration guidelines, it assists developers in effectively monitoring SQL execution for debugging and optimizing Hibernate applications.

Basic Configuration for Hibernate SQL Logging

During Hibernate application development, debugging generated SQL queries is a common requirement. By default, Hibernate uses prepared statements where parameter values appear as question mark placeholders in SQL, making it difficult to understand the actual executed queries during debugging.

To display complete SQL statements with their parameter values, the most straightforward approach is to configure appropriate logging levels. Specifically, two key logger categories need to be set with corresponding log levels:

Log4j Configuration Example

The following is a complete Log4j configuration example demonstrating how to enable Hibernate SQL and parameter logging:

# Enable Hibernate SQL statement logging
log4j.logger.org.hibernate.SQL=debug

# Enable Hibernate type system logging to show parameter binding
log4j.logger.org.hibernate.type=trace

# Optional: Configure log output format
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Limitations of Traditional Configuration Properties

Hibernate provides the hibernate.show_sql configuration property, which when set to true outputs SQL statements to the console. However, this method has significant limitations:

In contrast, configuring the org.hibernate.type category through logging frameworks provides complete parameter binding information, including parameter types, positions, and actual values.

Analysis of Log Output Format

After enabling parameter logging, the typical output format appears as follows:

update FlightEntry set version=?, companyid=?, entryid=?, flightnumber=?, registrationnumber=?, scheduled_arrival_time=?, scheduled_departure_time=?, weekdays=?, handling_time=?, followup_time=?, fueling_time=?, fuelrequest=?, quantity=?, secondvehicle=?, airplanetype_id=?, airline_id=? where id=? and version=?
[IntegerType] binding '1' to parameter: 1
[LongType] binding '1' to parameter: 2
[LongType] binding null to parameter: 3
[StringType] binding '2342' to parameter: 4

While this format is detailed, for queries with numerous parameters, the output becomes verbose and difficult to read. Ideally, we would prefer a more concise format such as:

update FlightEntry set version=1, companyid=1, entryid=null, flightnumber=2342, ... where id=1 and version=0

Alternative Solution: JDBC Proxy Drivers

For scenarios requiring more concise output formats or advanced logging capabilities, consider using JDBC proxy drivers like P6Spy. P6Spy intercepts JDBC calls and can log SQL statements with parameter values in customizable formats.

Key advantages of P6Spy include:

Configuring P6Spy typically involves the following steps:

  1. Add P6Spy JAR file to the classpath
  2. Modify JDBC URL to use P6Spy driver
  3. Configure P6Spy properties file to define output format and behavior

Performance Considerations and Best Practices

Enabling detailed SQL logging in production environments may significantly impact performance, particularly in high-concurrency scenarios. Here are some recommended best practices:

By properly configuring Hibernate logging or using specialized JDBC monitoring tools, developers can effectively debug SQL queries, optimize application performance, and ensure the correctness of the data access layer.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.