在Hibernate中是否有可能用真实值而不是问号打印生成的SQL查询?

如果无法使用Hibernate API,您会建议如何使用实际值打印查询?


当前回答

你可以记录这个:net.sf.hibernate.hql.QueryTranslator

输出的例子:

2013-10-31 14:56:19,029 DEBUG [net.sf.hibernate.hql.QueryTranslator] HQL: select noti.id, noti.idmicrosite, noti.fcaducidad, noti.fpublicacion, noti.tipo, noti.imagen, noti.visible, trad.titulo, trad.subtitulo, trad.laurl, trad.urlnom, trad.fuente, trad.texto  from org.ibit.rol.sac.micromodel.Noticia noti join noti.traducciones trad where index(trad)='ca' and noti.visible='S' and noti.idmicrosite=985 and noti.tipo=3446       
2013-10-31 14:56:19,029 DEBUG [net.sf.hibernate.hql.QueryTranslator] SQL: select noticia0_.NOT_CODI as x0_0_, noticia0_.NOT_MICCOD as x1_0_, noticia0_.NOT_CADUCA as x2_0_, noticia0_.NOT_PUBLIC as x3_0_, noticia0_.NOT_TIPO as x4_0_, noticia0_.NOT_IMAGEN as x5_0_, noticia0_.NOT_VISIB as x6_0_, traduccion1_.NID_TITULO as x7_0_, traduccion1_.NID_SUBTIT as x8_0_, traduccion1_.NID_URL as x9_0_, traduccion1_.NID_URLNOM as x10_0_, traduccion1_.NID_FUENTE as x11_0_, traduccion1_.NID_TEXTO as x12_0_ from GUS_NOTICS noticia0_ inner join GUS_NOTIDI traduccion1_ on noticia0_.NOT_CODI=traduccion1_.NID_NOTCOD where (traduccion1_.NID_CODIDI='ca' )and(noticia0_.NOT_VISIB='S' )and(noticia0_.NOT_MICCOD=985 )and(noticia0_.NOT_TIPO=3446 )

其他回答

您可以在log4j.xml中添加类别行:

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

并添加hibernate属性:

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

如果您正在使用hibernate 3.2。X使用这个:

log4j.logger.org.hibernate.SQL=trace

而不是这样:

log4j.logger.org.hibernate.SQL=debug 

使用Wireshark或类似的工具:

上面提到的答案都不会正确地打印带参数的SQL,或者让它与它们一起工作是一件痛苦的事情。我通过使用WireShark实现了这一点,它可以捕获从应用程序发送到Oracle/MySQL等的所有SQL/命令。

这个答案对这个问题有点不同。 有时,我们只需要sql在运行时进行调试。 在这种情况下,有一种更简单的方法,在编辑器上使用调试。

在org.hibernate.loader.Loader.loadEntityBatch上放置一个断点(或在堆栈上导航到那里); 当执行暂停时,查看变量this.sql的值

这是针对Hibernate 3的。我不确定这是否适用于其他版本。

Hibernate在不同的行中显示查询及其参数值。

如果你正在使用应用程序。你可以在application.properties中使用下面突出显示的参数。

org.hibernate.SQL will show queries: logging.level.org.hibernate.SQL=DEBUG org.hibernate.type will show all parameter values, which will map with select, insert and update queries. logging.level.org.hibernate.type=TRACE org.hibernate.type.EnumType will show enum type parameter value: logging.level.org.hibernate.type.EnumType=TRACE Example output: 2018-06-14 11:06:28,217 TRACE [main] [EnumType.java : 321] Binding [active] to parameter: [1] sql.BasicBinder will show integer, varchar, boolean type parameter value logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE Example output: * 2018-06-14 11:28:29,750 TRACE [http-nio-9891-exec-2] [BasicBinder.java : 65] binding parameter [1] as [BOOLEAN] - [true] * 2018-06-14 11:28:29,751 TRACE [http-nio-9891-exec-2] [BasicBinder.java : 65] binding parameter [2] as [INTEGER] - [1] * 2018-06-14 11:28:29,752 TRACE [http-nio-9891-exec-2] [BasicBinder.java : 65] binding parameter [3] as [VARCHAR] - [public]