How to log query parameters passed to JPA queries


I am using JPA with hibernate underneath. Using the property

<property name="hibernate.show_sql" value="true"/>

Shows just the SQL and not the values of parameters passed -it displays ? marks for query parameter as incomeexpe0_.transactiondate)>=?
Want to know what was the exact parameters passed -This is real helpful for debugging.
This can be achieved using hibernate logging -Add following two lines in your log4J.properties file

log4j.logger.org.hibernate.SQL=TRACE
log4j.logger.org.hibernate.type=TRACE

If this is used “log4j.logger.org.hibernate.SQL=TRACE” -No need to use property hibernate.show_sql, it wil take care of dumping queries.
The secondstatement, “log4j.logger.org.hibernate.type”, logs the JDBC parameters passed to a query

TRACE [main] (BasicBinder.java:83) - binding parameter [1] as [TIMESTAMP] - Thu Jan 03 01:00:18 IST 2013
TRACE [main] (BasicBinder.java:83) - binding parameter [2] as [TIMESTAMP] - Thu Jan 03 23:00:19 IST 2013

For even more advanced analysis and precise JDBC formatted queries (Non in an ORM form, but REAL sql), jdbc proxy driver like P6Spy can be used.

About these ads

About ChandanPandey

Try to come up with a good design as by product of good coding practices

Posted on January 7, 2013, in Hibernate, Java and tagged , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: