在Hibernate中是否有可能用真实值而不是问号打印生成的SQL查询?
如果无法使用Hibernate API,您会建议如何使用实际值打印查询?
在Hibernate中是否有可能用真实值而不是问号打印生成的SQL查询?
如果无法使用Hibernate API,您会建议如何使用实际值打印查询?
当前回答
日志可以工作,但并不是您想要的,但P6Spy可以完美地工作。
这里有一个简单的教程来实现以及。对我来说,这就像魅力。
下载P6Spy库:获取P6Spy -install.jar 解压:解压p6spy-install.jar文件,查找p6spy.jar和spy.properties 添加库依赖项:将p6spy.jar添加到项目库依赖项中 Modify P6Spy properties file:修改数据库配置文件。您需要用P6Spy JDBC驱动程序替换现有的JDBC驱动程序。
原来是MySQL JDBC驱动程序- (com.mysql.jdbc.Driver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
更改为P6Spy JDBC驱动程序(com.p6spy.engine.spy.P6SpyDriver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
修改P6Spy属性文件:spy.properties
用现有的MySQL JDBC驱动程序替换realdriver
realdriver=com.mysql.jdbc.Driver
# specifies another driver to use
realdriver2=
# specifies a third driver to use
realdriver3=
在logfile属性中更改日志文件的位置。所有SQL语句都将被记录到这个文件中。
窗口:
logfile=c:/spy.log
UNIX:
logfile=/srv/log/spy.log
复制的间谍。属性到项目类路径:复制间谍。属性添加到项目根文件夹。确保你的项目可以定位间谍。财产,否则就是间谍。属性文件未找到异常将被抛出。
其他回答
如果你想让Hibernate用真实值而不是问号打印生成的SQL查询,请在Hibernate .cfg.xml/ Hibernate .properties中添加以下条目:
show_sql=true
format_sql=true
use_sql_comments=true
并在log4j.properties中添加以下条目:
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
您需要配置如下:
# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=INFO
# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=trace
日志可以工作,但并不是您想要的,但P6Spy可以完美地工作。
这里有一个简单的教程来实现以及。对我来说,这就像魅力。
下载P6Spy库:获取P6Spy -install.jar 解压:解压p6spy-install.jar文件,查找p6spy.jar和spy.properties 添加库依赖项:将p6spy.jar添加到项目库依赖项中 Modify P6Spy properties file:修改数据库配置文件。您需要用P6Spy JDBC驱动程序替换现有的JDBC驱动程序。
原来是MySQL JDBC驱动程序- (com.mysql.jdbc.Driver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
更改为P6Spy JDBC驱动程序(com.p6spy.engine.spy.P6SpyDriver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
修改P6Spy属性文件:spy.properties
用现有的MySQL JDBC驱动程序替换realdriver
realdriver=com.mysql.jdbc.Driver
# specifies another driver to use
realdriver2=
# specifies a third driver to use
realdriver3=
在logfile属性中更改日志文件的位置。所有SQL语句都将被记录到这个文件中。
窗口:
logfile=c:/spy.log
UNIX:
logfile=/srv/log/spy.log
复制的间谍。属性到项目类路径:复制间谍。属性添加到项目根文件夹。确保你的项目可以定位间谍。财产,否则就是间谍。属性文件未找到异常将被抛出。
使用Hibernate 4和slf4j/log4j2,我尝试将以下内容添加到我的log4j2.xml配置:
<Logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.hibernate.type.EnumType" level="trace" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
但是没有成功。
通过这个线程,我发现Hibernate使用的jboss日志框架需要配置,以便通过slf4j进行日志记录。我将以下参数添加到应用程序的VM参数中:
-Dorg.jboss.logging.provider=slf4j
而且效果很好。
这个答案对这个问题有点不同。 有时,我们只需要sql在运行时进行调试。 在这种情况下,有一种更简单的方法,在编辑器上使用调试。
在org.hibernate.loader.Loader.loadEntityBatch上放置一个断点(或在堆栈上导航到那里); 当执行暂停时,查看变量this.sql的值
这是针对Hibernate 3的。我不确定这是否适用于其他版本。