Monday, October 10, 2016

Hibernate show sql with parameter values

In this article i will show you "How to log the SQL with parameters in Hibernate?"

As we know Hibernate is ORM framework which is being used at persistence layer. If you are a developer and you are Woking on development environment than i guess you faced a challenge to debug the SQL statement. 


Hibernate gives us a feature to show sql in the logs 



But the above feature doesn't let see the parameters which you are passing into your SQL statement. 


Hibernate provides 2 category and levels to execute the SQL parameters and show their bind parameters.


1. Use org.hibernate.SQL as debug

2. org.hibernate.type.descriptor.sql as trace

Here is the code snippet of log4j : - 



Please have look on the logs where you can see the actual parameters are being printed 




Employee entity used in the code : - 




Main class : - 




Summary :-


So using "log4j.logger.org.hibernate.SQL=debug" and "log4j.logger.org.hibernate.type.descriptor.sql=trace" we can easily log SQL parameters. With these features it is really easy to trouble shoot or debug the issue on development environment. But always remember it is always being advisable in the development or sand box environment. Please do not use this features on production environment because it will generate a lot of unnecessary logs and may creak disk space issue in the long run.

2 comments:

  1. I did all of the above and am still not seeing the bind parameters in my log prints. WTF were the hibernate people thinking? Just give me the goddamn query it's sending to the DB, it shouldn't be rocket science!

    ReplyDelete
  2. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

    ReplyDelete