Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SQL String from Hibernate query

I need to get the string from an Hibernate query and process it later (so I can't solve it with "hibernate.show_sql").

I have already looked at How to get SQL from Hibernate Criteria API (*not* for logging) but with that workaround I get the SQL query string but instead of showing the parameters value it shows '?'... Is there any way to get the full SQL string with the parameters values?

I mean, with that solution I get "SELECT * FROM USER WHERE NAME=? AND SURNAME=?" but I need to get "SELECT * FROM USER WHERE NAME='John' AND SURNAME='Doe'"...

Ideas?

like image 222
diminuta Avatar asked Jul 30 '12 08:07

diminuta


1 Answers

You have to set TRACE level of logging to this hibernate package and parameter binding should show in your application log:

<category name="org.hibernate.type">
      <priority value="TRACE"/>
</category>

Output example:

13:58:51,505 DEBUG [SQL] 
 insert 
        into
            s.audit
            (action, e_s, ip, time, userid, id) 
        values
            (?, ?, ?, ?, ?, ?)
13:58:51,505 TRACE [StringType] binding 'Modify user' to parameter: 1
13:58:51,505 TRACE [StringType] binding 'E' to parameter: 2
13:58:51,505 TRACE [StringType] binding '164.20.81.65' to parameter: 3
13:58:51,505 TRACE [TimestampType] binding '2012-07-30 13:58:51' to parameter: 4
13:58:51,505 TRACE [IntegerType] binding '158' to parameter: 5
13:58:51,505 TRACE [IntegerType] binding '8851' to parameter: 6

And don't forget 'hibernate.show_sql=true' property you said previously to show also the related SQL.

like image 133
jelies Avatar answered Sep 27 '22 17:09

jelies