使用 Hibernate 时如何打印带有参数值的查询字符串 [英] How to print a query string with parameter values when using Hibernate

查看:27
本文介绍了使用 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 to debug to log all SQL DML statements as they are executed
  • org.hibernate.type - set to trace 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆