Pawan
Pawan

Reputation: 273

print hibernate sql query string with parameters into logs

In my hibernate config show_sql property is 'true' and since i am not using log4j API of logging, by default hibernate was showing queries with '?' marks in the tomcat console.

In my case i need to print the complete sql query with parameter values which was executed by hibernate into logs, but i am not using log4j API for logging to configure the debug level for org.hibernate.SQL and org.hibernate.type.

For logging we are manually writing the content into the file, so in this situation can anybody tell me how can i get the hibernate queries into logs.

Upvotes: 1

Views: 11587

Answers (3)

flags
flags

Reputation: 511

The accepted answer was given almost 13 years ago. It's been a long time since then and things have changed. In hibernate 6.1.6.Final, to get this to work, you must use something like:

org.hibernate.SQL: trace
org.hibernate.orm.jdbc.bind: trace

The FQN of the BasicBinder class is now org.hibernate.type.descriptor.jdbc.BasicBinder

Upvotes: 0

Rahul Wagh
Rahul Wagh

Reputation: 494

As you have asked "To print the sql query with parameters values which was executed by hibernate into logs"

you can set the following parameters into the log4j.properties file

#following parameters will be used to log the sql parameters
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type.descriptor.sql=trace

It will print the query along with sql parameters along with the datatype .

Hibernate: insert into employee (employee_address, employee_name, id) values (?, ?, ?)
11:50:40, 209 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [1] as [VARCHAR] - [Address 0]
11:50:40, 209 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [2] as [VARCHAR] - [Employee 0]
11:50:40, 209 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [3] as [BIGINT] - [7008]
11:50:40, 210 DEBUG [org.hibernate.SQL] - insert into employee (employee_address, employee_name, id) values (?, ?, ?)

Please visit this post for more details about code : - http://techpost360.blogspot.in/2016/10/hibernate-show-sql-with-parameter-values.html

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570365

In my case i need to print the complete sql query with parameter values which was executed by hibernate into logs, but i am not using log4j API for logging to configure the debug level for org.hibernate.SQL and org.hibernate.type

Whether you're using a logging framework or not, Hibernate will output question marks when logging prepared statements.

If you want to print the "real query" with the bound values, you'll have to use a JDBC Proxy driver like P6Spy (doesn't move anymore) or log4jdbc.

Upvotes: 0

Related Questions