Hibernate

时间:2016-11-08 09:03:33

标签: spring hibernate logging log4j hql

我想在我的日志中显示由ihbernate执行的sql查询。

这是我当前的log4j配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

 <appender name="CA" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n" />
  </layout>
 </appender>

 <root>
  <level value="info" />
  <appender-ref ref="CA" />
 </root>

<logger name="org.hibernate.SQL" additivity="false">
  <level value="debug" />
  <appender-ref ref="CA" />
 </logger>

 <logger name="org.hibernate.hql" additivity="false">
  <level value="debug" />
  <appender-ref ref="CA" />
 </logger>

 <logger name="org.hibernate.stat" additivity="false">
  <level value="trace" />
  <appender-ref ref="CA" />
 </logger>

</log4j:configuration>

但我仍然使用org.hibernate.stat.internal.ConcurrentStatisticsImpl获取空值:

08-11-2016 09:54:39,318 [http-bio-8080-exec-8] DEBUG org.hibernate.stat.internal.ConcurrentStatisticsImpl  - HHH000117: HQL: null, time: 1ms, rows: 4

如何记录完整的SQL而不是HQL: null

1 个答案:

答案 0 :(得分:0)

要记录执行的查询,我将以下内容添加到我的logj4配置中:

<!-- log Hibernate SQL Statements without parameters -->
 <logger name="org.hibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="console" />
</logger>
<!-- add logging of parameters of the sql statements -->
<logger name="org.hibernate.type.descriptor.sql" additivity="false">
    <level value="TRACE" />
    <appender-ref ref="console" />
</logger>