Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log values that Hibernate binds to prepared statements?

How can I make Hibernate log the values it binds to prepared statements?

If I set property hibernate.show_sql=true I get following kind of logging:

insert into tablename (field1, field2) values (?, ?)

I'd like also to know what values are bound to question marks.

I am using Hibernate 3.2.7.ga.

like image 680
Juha Syrjälä Avatar asked Sep 10 '10 07:09

Juha Syrjälä


People also ask

Does hibernate use prepared statements?

Hibernate executes all SQL queries and DML operations using prepared statements. Not only that prepared statements help to prevent SQL injection attacks, but they can help speed up query executions, especially when the underlying database provides an execution plan cache (e.g. Oracle, SQL Server).

How does hibernate log SQL statements and their bind parameters?

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.

How do you configure hibernate to write the executed SQL statements?

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.

How do I log a Hibernate Query?

log4j. logger. org. hibernate. SQL= DEBUG This will log the Hibernate queries in the following manner, with question marks as place holders for bound parameters.

What is a hibernate log category?

Hibernate writes the DDL SQL queries executed during schema migration to this log category. The names of the log levels are defined by your logging framework and define the amount and granularity of the log messages. You can assign a log level to each category.


1 Answers

You need to set the following logging category to TRACE

org.hibernate.type

And personally, I don't use the show_sql property, I work with DEBUG enabled for the category:

org.hibernate.SQL

This way, I have a single place to configure everything.

Reference

  • Hibernate Core Reference Guide
    • 3.5. Logging
like image 61
Pascal Thivent Avatar answered Oct 03 '22 14:10

Pascal Thivent