Don’t want to read? You can watch it here!
How do you configure Hibernate so that it writes the executed SQL statements and used bind parameters to the log file?
Hibernate uses 2 different log categories and log levels to log the executed SQL statements and their bind parameters:
- The SQL statements are written as DEBUG messages to the category org.hibernate.SQL.
- The bind parameters are logged to the org.hibernate.type.descriptor.sql category with log level TRACE.
You can activate and deactivate them independently of each other in your log configuration.
The following code snippet shows an example of a log4j configuration which activates both of them.
Hibernate then writes log messages like the following one to your log file.
Get this Hibernate Tip as a printable PDF!Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
You can find more information about Hibernates logging features and my recommendation for a development and a production configuration (yes, you should use 2 different ones!) in Hibernate Logging Guide – Use the right config for development and production.
Hibernate Tips Book
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.
Get it now as a paperback, ebook or PDF.