1

I want to print sql queries generated by hibernate to console.

insert into ORDERS (CRETN_DT, CRETN_USER_ID, LAST_UPDT_DT, LAST_UPDT_USER_ID, ORDER_ID) values (?, ?, ?, ?, ?)

This is what I see on console. Instead of ? , is it possible to print the actual value? Instead of printing the parameter values below the query , i would like to replace the ?s with the actual value which can be executed as is against the db.

Punter Vicky
  • 12,516
  • 39
  • 134
  • 241
  • this will generate the query and the parameters no? – YCF_L Apr 20 '18 at 21:02
  • Thanks for the link. However I would like the ?s to be replaced with actual value. The approaches listed seem to list the parameters below the query. – Punter Vicky Apr 21 '18 at 01:13
  • That is probably because it is not possible. For most database drivers, the query is prepared with those parameter placeholders, and the values are always sent separately. There never is a query with those values within the query instead of the parameter values, so most tools don't try to generate that because it is not very useful (error-prone, extra code maintenance, a query generated that way may not actually be valid for the targeted database,etc). – Mark Rotteveel Apr 21 '18 at 19:45
  • Try logback. In the logback.xml use next settings, to log hibernate bind values : – Lev Apr 22 '18 at 11:57

1 Answers1

1

You can use datasource-proxy or p6spy

This allows you to view the actual parameter values used in firing the SQL. Example on how to configure can be found here

Sample format would be as below:

Name:DATA_SOURCE_PROXY, Time:6, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:3, Query:["insert into post (title, version, id) values (?, ?, ?)"], Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)