开发者

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 loggin开发者_如何学Pythong, 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜