使用 Hibernate 时如何打印带有参数值的查询字符串 [英] How to print a query string with parameter values when using Hibernate
问题描述
是否可以在 Hibernate 中使用实际值而不是问号打印生成的 SQL 查询?
如果 Hibernate API 无法实现,您如何建议打印具有真实值的查询?
您需要启用 logging 用于以下类别:
org.hibernate.SQL
- 设置为debug
以在执行时记录所有 SQL DML 语句org.hibernate.type
- 设置为trace
以记录所有 JDBC 参数
所以 log4j 配置可能如下所示:
# 记录 SQL 语句log4j.logger.org.hibernate.SQL=调试# 记录传递给查询的 JDBC 参数log4j.logger.org.hibernate.type=trace
第一个相当于 hibernate.show_sql=true
legacy property,第二个打印绑定参数等.
另一种解决方案(非基于休眠的)是使用 JDBC 代理驱动程序,例如 P6Spy.>
Is it possible in Hibernate to print generated SQL queries with real values instead of question marks?
How would you suggest to print queries with real values if it is not possible with Hibernate API?
You need to enable logging for the the following categories:
org.hibernate.SQL
- set todebug
to log all SQL DML statements as they are executedorg.hibernate.type
- set totrace
to log all JDBC parameters
So a log4j configuration could look like:
# 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 is equivalent to hibernate.show_sql=true
legacy property, the second prints the bound parameters among other things.
Another solution (non hibernate based) would be to use a JDBC proxy driver like P6Spy.
这篇关于使用 Hibernate 时如何打印带有参数值的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!