Print query containing parameter values in Hibernate
When using Hibernate, it is often necessary to view the actual SQL query executed, including parameter values. Hibernate's default behavior is to log SQL queries using question marks (?) as placeholders for parameter values.
Is it possible to print a query containing actual values using Hibernate API?
No, Hibernate does not provide an API to print SQL queries containing actual values.
How to print query containing actual values
For this feature to work, logging needs to be enabled for the following categories:
<code>org.hibernate.SQL - 设置为debug以记录所有SQL DML语句的执行情况 org.hibernate.type - 设置为trace以记录所有JDBC参数</code>
Log4j configuration example:
<code># 记录SQL语句 log4j.logger.org.hibernate.SQL=debug # 记录传递给查询的JDBC参数 log4j.logger.org.hibernate.type=trace</code>
The first configuration is equivalent to Hibernate's old property hibernate.show_sql=true, and the second configuration prints binding parameters and other information.
Alternatives
If Hibernate logging is insufficient, you can use a JDBC proxy driver such as P6Spy to capture and log SQL queries containing parameter values.
The above is the detailed content of How can I log Hibernate SQL queries with their actual parameter values?. For more information, please follow other related articles on the PHP Chinese website!