Home > Database > Mysql Tutorial > How Can I See the Actual SQL Generated by Hibernate?

How Can I See the Actual SQL Generated by Hibernate?

Susan Sarandon
Release: 2025-01-21 07:47:11
Original
716 people have browsed it

How Can I See the Actual SQL Generated by Hibernate?

Hibernate displays real SQL

Set the hibernate.cfg.xml attribute to show_sql in Hibernate's true configuration file to view the SQL statements generated by Hibernate. However, the generated SQL may not always be in an easy-to-read format.

View actual SQL

To see the actual SQL statement passed directly to the database, you can:

  • Use a JDBC driver proxy such as P6Spy or log4jdbc.
  • Use log4j.properties files to enable logging for the following categories:
<code>log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE</code>
Copy after login

The first category is equivalent to hibernate.show_sql=true, while the second category prints binding parameters and other information.

Example

Using the second method, you may see the following output:

<code>2023-02-27 16:01:18,226 DEBUG org.hibernate.SQL  - select employee.code from employee where employee.code = ?
2023-02-27 16:01:18,232 TRACE org.hibernate.type.descriptor.sql.BasicBinder  - binding parameter [1] as [INTEGER] - [12]</code>
Copy after login

This will give you "real" SQL, similar to:

<code>select employee.code from employee where employee.code=12</code>
Copy after login

Reference

The above is the detailed content of How Can I See the Actual SQL Generated by 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