3

I want to do audit logging generated SQL statements in Hibernate. I wrote Custom Interceptor that extents EmptyInterceptor. I overrided onPrepareStatement method to get generated SQL. But When I debug this code I am only getting generated SQL without parameters like this.

INSERT INTO TableA(Col1,Col2,Col3) VALUES(?,?,?)

How can I get full SQL statement that contains parameter values like this from Hibernate

INSERT INTO TableA(Col1,Col2,Col3) VALUES("Value1","Value2","Value3")

4 Answers 4

2

In hibernate cfg, you may set hibernate.show_sql property to true, this will cause Hibernate to output the perpared statements (without the parameter values bound to a query) to stdout.

To have the precompiled statements and list of parameters bound to them logged, set log4j.logger.org.hibernate.SQL=DEBUG in your commons-logging (or underlying log system) configuration.

Sign up to request clarification or add additional context in comments.

Comments

2

The accepted answer did not work for me (Postgres version 8.3.7-1, driver version 8.1-407).

I had to also set the "type" option to DEBUG in the logging configuration:

log4j.logger.org.hibernate.type=DEBUG

Comments

0

It looks like, depending on your version of log4j this is still a bug

https://hibernate.onjira.com/browse/HHH-2835

Comments

0

How about using one of the many "JDBC wrappers" out there meant for this?

In no particular order:

  1. P6Spy - http://sourceforge.net/projects/p6spy/
  2. log4jdbc - http://code.google.com/p/log4jdbc/
  3. JDBCLogger - http://jdbclogger.sourceforge.net/
  4. and many, many more

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.