string 从 Hibernate 查询中获取 SQL 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11717888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:33:15  来源:igfitidea点击:

Get SQL String from Hibernate query

stringhibernatejakarta-ee

提问by diminuta

I need to get the string from an Hibernate query and process it later (so I can't solve it with "hibernate.show_sql").

我需要从 Hibernate 查询中获取字符串并稍后处理它(所以我无法用 解决它"hibernate.show_sql")。

I have already looked at How to get SQL from Hibernate Criteria API (*not* for logging)but with that workaround I get the SQL query string but instead of showing the parameters value it shows '?'... Is there any way to get the full SQL string with the parameters values?

我已经看过如何从 Hibernate Criteria API (*not* for logging) 中获取 SQL,但是使用该解决方法,我得到了 SQL 查询字符串,但没有显示参数值,而是显示 '?'... 有什么办法可以获取带有参数值的完整 SQL 字符串?

I mean, with that solution I get "SELECT * FROM USER WHERE NAME=? AND SURNAME=?"but I need to get "SELECT * FROM USER WHERE NAME='John' AND SURNAME='Doe'"...

我的意思是,有了这个解决方案,我得到了"SELECT * FROM USER WHERE NAME=? AND SURNAME=?"但我需要得到"SELECT * FROM USER WHERE NAME='John' AND SURNAME='Doe'"......

Ideas?

想法?

采纳答案by sorencito

There is a tool called p6spy. It's a bit older and I am afraid it isn't maintained anymore. It gave me good results in the past, anyway.

有一个名为p6spy的工具。它有点旧,恐怕不再维护了。无论如何,它在过去给了我很好的结果。

Edit: just found that it is being actively developed again. Works like a charm for me!

编辑:刚刚发现它正在再次积极开发。对我来说就像一个魅力!

回答by jelies

You have to set TRACElevel of logging to this hibernate package and parameter binding should show in your application log:

您必须TRACE为此休眠包设置日志记录级别,并且参数绑定应显示在您的应用程序日志中:

<category name="org.hibernate.type">
      <priority value="TRACE"/>
</category>

Output example:

输出示例:

13:58:51,505 DEBUG [SQL] 
 insert 
        into
            s.audit
            (action, e_s, ip, time, userid, id) 
        values
            (?, ?, ?, ?, ?, ?)
13:58:51,505 TRACE [StringType] binding 'Modify user' to parameter: 1
13:58:51,505 TRACE [StringType] binding 'E' to parameter: 2
13:58:51,505 TRACE [StringType] binding '164.20.81.65' to parameter: 3
13:58:51,505 TRACE [TimestampType] binding '2012-07-30 13:58:51' to parameter: 4
13:58:51,505 TRACE [IntegerType] binding '158' to parameter: 5
13:58:51,505 TRACE [IntegerType] binding '8851' to parameter: 6

And don't forget 'hibernate.show_sql=true'property you said previously to show also the related SQL.

并且不要忘记'hibernate.show_sql=true'您之前所说的用于显示相关 SQL 的属性。

回答by axtavt

There is no such thing as "the full SQL string with the parameters values".

没有“带有参数值的完整 SQL 字符串”这样的东西。

Hibernate uses prepared statements, therefore it sends query string with ?s and parameter values to the database separately, so that the actual query is never constructed.

Hibernate 使用准备好的语句,因此它将带有?s 和参数值的查询字符串分别发送到数据库,因此永远不会构造实际的查询。

Perhaps you can find a way extract parameter values from QueryImpl, but even in this case you won't be able to get a query ready for execution, because you'll need to handle escaping, conversion of parameter values to SQL literals, etc.

也许您可以找到一种从 中提取参数值的方法QueryImpl,但即使在这种情况下,您也无法准备好执行查询,因为您需要处理转义、将参数值转换为 SQL 文字等。

回答by bluesockets

JdbcLogger does this form you. http://jdbclogger.sourceforge.net/. Supports mysql, postgres, and spring integration.

JdbcLogger 为您执行此操作。http://jdbclogger.sourceforge.net/。支持 mysql、postgres 和 spring 集成。

回答by Roman Konoval

Another option is to use datasource-proxywhich allows to listen to different JDBC events including query execution.

另一种选择是使用数据源代理,它允许侦听不同的 JDBC 事件,包括查询执行。

You can implement QueryExecutionListener.beforeQuerythat receives QueryInfowith SQL query text and all parameters.

您可以实现QueryExecutionListener.beforeQuery接收QueryInfoSQL 查询文本和所有参数。