Home >Database >Mysql Tutorial >How to Log Detailed SQL Queries with Parameter Values in Hibernate?

How to Log Detailed SQL Queries with Parameter Values in Hibernate?

DDD
DDDOriginal
2025-01-22 22:32:10948browse

How to Log Detailed SQL Queries with Parameter Values in Hibernate?

Print detailed SQL log containing actual parameter values

When using Hibernate, it is often useful to view the SQL queries that are executed, especially when debugging or optimizing performance. However, by default, Hibernate logs SQL statements using placeholders (question marks) instead of the actual values ​​of query parameters.

Hibernate log configuration

If you need to print queries that contain actual values, you can configure the following categories of logging:

  • org.hibernate.SQL: Set to debug to log all executed SQL DML statements.
  • org.hibernate.type: Set to trace to log all JDBC parameters.

For example, using log4j configuration:

<code># 记录SQL语句
log4j.logger.org.hibernate.SQL=debug 

# 记录传递给查询的JDBC参数
log4j.logger.org.hibernate.type=trace </code>

The first setting (org.hibernate.SQL) is equivalent to setting the hibernate.show_sql=true legacy property. The second setting (org.hibernate.type) prints binding parameters and other information.

Non-Hibernate solution

Another way to print queries containing actual values ​​is to use a JDBC proxy driver such as P6Spy. This driver intercepts all SQL statements and logs information with detailed information, including the actual values ​​of query parameters.

The above is the detailed content of How to Log Detailed SQL Queries with Parameter Values in Hibernate?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn