Is it possible in Hibernate to print generated SQL queries with real values instead of question marks?
How would you suggest to print queries with real values if it is not possible with Hibernate API?
Is it possible in Hibernate to print generated SQL queries with real values instead of question marks?
How would you suggest to print queries with real values if it is not possible with Hibernate API?
You need to enable logging for the the following categories:
org.hibernate.SQL
- set todebug
to log all SQL DML statements as they are executedorg.hibernate.type
- set totrace
to log all JDBC parametersSo a log4j configuration could look like:
The first is equivalent to
hibernate.show_sql=true
legacy property, the second prints the bound parameters among other things.Another solution (non hibernate based) would be to use a JDBC proxy driver like P6Spy.
This answer is a little variance for the question. Sometimes, we only need the sql only for debug purposes in runtime. In that case, there are a more easy way, using debug on editors.
This is for hibernate 3. I'm not sure that this work on other versions.
The simplest solution for me is implementing a regular stringReplace to replace parameter inputs with parameter values (treating all parameters as string, for simplicity):
or something similar for positional parameters (?).
Take care of null values and specific value types like date, if you want a run ready sql to be logged.
Log4Jdbc plugin would be best for your requirement. It shows following-
Refer below link to configure Log4Jdbc-
add following properties and values to your log4j or logback configuration:
I like this for log4j: