caltuntas
caltuntas

Reputation: 10852

Hibernate SQL Audit Logging

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")

Upvotes: 3

Views: 2530

Answers (4)

Steve Ebersole
Steve Ebersole

Reputation: 9443

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

Upvotes: 0

user833970
user833970

Reputation: 2799

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

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

Upvotes: 0

jwaddell
jwaddell

Reputation: 1924

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

Upvotes: 2

david a.
david a.

Reputation: 5291

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.

Upvotes: 2

Related Questions