Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate SQL Audit Logging

Tags:

hibernate

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")
like image 228
caltuntas Avatar asked Feb 02 '26 00:02

caltuntas


2 Answers

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.

like image 67
david a. Avatar answered Feb 03 '26 13:02

david a.


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
like image 27
jwaddell Avatar answered Feb 03 '26 13:02

jwaddell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!