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.
Posted on January 7, 2013, in Hibernate, Java and tagged dump sql, hibernate, JPA, log query parameters. Bookmark the permalink. 1 Comment.
How about OpenJPA ?