Home > Database > Mysql Tutorial > How can I log Hibernate SQL queries with their actual parameter values?

How can I log Hibernate SQL queries with their actual parameter values?

Linda Hamilton
Release: 2025-01-22 22:58:12
Original
269 people have browsed it

How can I log Hibernate SQL queries with their actual parameter values?

Print query containing parameter values ​​in Hibernate

When using Hibernate, it is often necessary to view the actual SQL query executed, including parameter values. Hibernate's default behavior is to log SQL queries using question marks (?) as placeholders for parameter values.

Is it possible to print a query containing actual values ​​using Hibernate API?

No, Hibernate does not provide an API to print SQL queries containing actual values.

How to print query containing actual values

For this feature to work, logging needs to be enabled for the following categories:

<code>org.hibernate.SQL - 设置为debug以记录所有SQL DML语句的执行情况
org.hibernate.type - 设置为trace以记录所有JDBC参数</code>
Copy after login

Log4j configuration example:

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

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

The first configuration is equivalent to Hibernate's old property hibernate.show_sql=true, and the second configuration prints binding parameters and other information.

Alternatives

If Hibernate logging is insufficient, you can use a JDBC proxy driver such as P6Spy to capture and log SQL queries containing parameter values.

The above is the detailed content of How can I log Hibernate SQL queries with their actual parameter values?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template