Home > Database > Mysql Tutorial > How Can I Log SQL Queries with Parameter Values in Hibernate?

How Can I Log SQL Queries with Parameter Values in Hibernate?

Barbara Streisand
Release: 2025-01-22 22:36:14
Original
873 people have browsed it

How Can I Log SQL Queries with Parameter Values in Hibernate?

Monitoring Hibernate SQL Queries and Parameters

Debugging Hibernate applications often requires inspecting the generated SQL queries, including their parameter values. Standard Hibernate logging only shows placeholder question marks instead of the actual values. This article details how to reveal these values.

Leveraging Hibernate's Built-in Logging

Hibernate offers two logging categories for detailed query visualization:

  • org.hibernate.SQL (debug level): Displays all executed SQL Data Manipulation Language (DML) statements.
  • org.hibernate.type (trace level): Exposes all JDBC parameters used in each query.

Enable this logging functionality by adding these lines to your log4j configuration file:

<code># Displays SQL statements
log4j.logger.org.hibernate.SQL=debug

# Displays JDBC parameters
log4j.logger.org.hibernate.type=trace</code>
Copy after login

Alternative: Using a JDBC Proxy Driver

For those who prefer an alternative to Hibernate's native logging, a JDBC proxy driver such as P6Spy offers a robust solution. This driver intercepts and modifies JDBC calls, providing a pre-execution view of queries for comprehensive analysis.

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

source:php.cn
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