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

查看:97
本文介绍了如何在使用Hibernate时打印带有参数值的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate中,是否可以使用实际值而不是问号打印生成的SQL查询?

如果不是真实值,您会如何建议打印查询?使用Hibernate API可能吗?

解决方案

您需要启用记录以下类别:


  • org.hibernate.SQL    - 设为 debug 在执行时记录所有SQL DML语句

  • org.hibernate.type - 设置为 trace code>来记录所有JDBC参数所以,一个log4j配置可能如下所示:



    $ b b $ b

     #记录SQL语句
    log4j.logger.org.hibernate.SQL = debug

    #记录传递给的JDBC参数查询
    log4j.logger.org.hibernate.type = trace

    第一个相当于 hibernate.show_sql = true 传统属性,第二个打印绑定参数。

    另一种解决方案(基于非hibernate)将使用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天全站免登陆