[英]How can I get a printout of a database query from Hibernate and Spring
I work on a huge Java project includes Spring and hibernate.我从事一个巨大的 Java 项目,包括 Spring 和 hibernate。
I was recently debugging some database query issues and drilled down until I discovered this is actually where a query is executed:我最近正在调试一些数据库查询问题并深入研究,直到我发现这实际上是执行查询的地方:
org.springframework.transaction.support.TransactionTemplate.execute(TransactionCallback<T>)
The code for the callback method it takes as the input argument is:作为输入参数的回调方法的代码是:
new TransactionCallback<List<?>>() {
@Override
public List<?> doInTransaction(TransactionStatus status) {
String queryString = query.createHQLQuery();
Query hibQuery = getCurrentSession().createQuery(queryString);
try {
query.populateHQLQuery(hibQuery, getSessionFactory());
} catch (DataAccessLayerException e) {
throw new org.hibernate.TransactionException("Error populating query", e);
}
// hibQuery.setCacheMode(CacheMode.NORMAL);
// hibQuery.setCacheRegion(QUERY_CACHE_REGION);
if (query.getMaxResults() != null) {
hibQuery.setMaxResults(query.getMaxResults());
}
List<?> results = hibQuery.list();
return results;
}
}
I was able to get printouts of the query to the database to debug the problem, but it was messy.我能够将查询的打印输出打印到数据库以调试问题,但它很混乱。 The best I could do was to call我能做的最好的事情就是打电话
org.hibernate.Query.getQueryString()
To get a template for a query, instead of the names of fields and values I got numbered substitution variables, ie " select param1, param2 from Yada where thisthing = param3".为了获得查询模板,我得到了编号的替代变量,而不是字段和值的名称,即“从 Yada 中选择 param1, param2 where thisthing = param3”。
I was able to call我可以打电话
org.hibernate.Query.getNamedParameters()
To get the values of the named parameters ( param1, param2, param3 ).获取命名参数的值( param1, param2, param3 )。
However, this was quite cumbersome as the queries were quite large and complicated.但是,这非常麻烦,因为查询非常大且复杂。
What I would like to know if there is a way to get a query to print to standard out, that already has the substitution variables replaced with the values, the named parameters.我想知道是否有一种方法可以将查询打印到标准输出,该方法已经将替换变量替换为值,即命名参数。 Basically, a complete query as someone would manually type into a database.基本上,一个完整的查询,因为有人会手动输入到数据库中。
In hibernate configuration在休眠配置中
If you are using JPA in persistence.xml如果您在 persistence.xml 中使用 JPA
<property name="hibernate.show_sql" value="true" />
If you want parameter values如果你想要参数值
Configure the Log4j in Hibernate在 Hibernate 中配置 Log4j
Modify the Log4j properties file, and change the log level to “debug” or “trace” in “log4j.logger.org.hibernate.type” property.修改Log4j属性文件,将“log4j.logger.org.hibernate.type”属性中的日志级别改为“debug”或“trace”。
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=INFO, stdout
# 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
Add to application properties添加到应用程序属性
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
To log values:要记录值:
spring.jpa.properties.hibernate.type=trace
If you want the statements written to your log, set the log level of org.hibernate.SQL
to DEBUG
.如果要将语句写入日志,请将org.hibernate.SQL
的日志级别设置为DEBUG
。 Bind parameters are logged if you set the log level of org.hibernate.type.descriptor.sql
to TRACE
.如果将org.hibernate.type.descriptor.sql
的日志级别设置为TRACE
则会记录绑定参数。
As other answers suggest using hibernate.show_sql
instructs Hibernate to write the SQL statements to STDOUT.由于其他答案建议使用hibernate.show_sql
指示 Hibernate 将 SQL 语句写入 STDOUT。 As a consequence this cannot be controlled by the logging framework of your choice.因此,这无法由您选择的日志记录框架控制。 Another major disadvantage is: the persistence.xml
is usually part of your application's JAR and cannot be changed without repackaging your application.另一个主要缺点是: persistence.xml
通常是应用程序 JAR 的一部分,如果不重新打包应用程序就无法更改。
Recommended reading: Hibernate Logging Guide – Use the right config for development and production推荐阅读: Hibernate Logging Guide – 使用正确的配置进行开发和生产
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.