Introduction:
When using Hibernate, it can be helpful to print out the queries that are being generated, along with the actual parameter values. This can be useful for debugging purposes, or for understanding the performance characteristics of a particular query.
Is it Possible with Hibernate API?
Yes, it is possible to print queries with parameter values using the Hibernate API. To do this, you need to enable logging for the following categories:
Configuration Example:
A log4j configuration that enables logging for these categories might look like this:
# logs the SQL statements log4j.logger.org.hibernate.SQL=debug # Logs the JDBC parameters passed to a query log4j.logger.org.hibernate.type=trace
The first option (hibernate.show_sql) is the legacy equivalent of log4j.logger.org.hibernate.SQL=debug, while the second option prints the bound parameters along with other information.
Alternative Non-Hibernate Solution:
If you prefer not to use the Hibernate API to print queries with parameter values, you can use a JDBC proxy driver like P6Spy. This driver acts as a proxy between your application and the database, and it logs all JDBC statements and parameters that pass through it.
The above is the detailed content of How Can I Print SQL Queries with Parameter Values in Hibernate?. For more information, please follow other related articles on the PHP Chinese website!