Home >Database >Mysql Tutorial >How Can I See the Exact SQL Hibernate Sends to the Database?

How Can I See the Exact SQL Hibernate Sends to the Database?

Linda Hamilton
Linda HamiltonOriginal
2025-01-21 07:36:11773browse

How Can I See the Exact SQL Hibernate Sends to the Database?

Unveiling Hibernate's Actual Database SQL

While setting show_sql to true in hibernate.cfg.xml reveals SQL statements, this output might differ from the database's actual execution.

Discrepancies Between Displayed and Executed SQL

The show_sql output undergoes modifications:

  • Entity names and Hibernate mappings alter table and column names.
  • Database-specific syntax is generalized.

Accessing the True SQL

Two methods reveal the precise SQL:

1. Employing a JDBC Driver Proxy

Tools such as P6Spy or log4jdbc act as intermediaries between your application and the database, capturing the exact SQL queries.

2. Leveraging Log4j Configuration

Configure log4j using log4j.properties as follows to log SQL queries and parameters:

<code>log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE</code>

Caution: Setting org.hibernate.type to TRACE generates extensive logs.

Further Reading:

The above is the detailed content of How Can I See the Exact SQL Hibernate Sends to the Database?. 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