Reputation: 199
We have a grails application with a legacy database. When a user logs into our app and does transactions we need to keep track of the sql that is occurring during the session. From a code standpoint is there a way to get the hql being ran when a .save() is called and translate this into sql? I have seen where you can turn on the logging for it but I would rather not try and parse this info out of the log files during runtime.
Upvotes: 0
Views: 597
Reputation: 1002
Approach 1 : You can log the sql by setting the below property in log4j.
log4j.logger.org.hibernate.SQL=debug
Approach 2:
You also can provide comments to SQL output. The Query interface has a setComment() method that takes a String object as an argument:
public Query setComment(String comment)
Use this to identify the SQL output in your application logs, if SQL logging is enabled. for instance
String hql = "from Supplier";
Query query = session.createQuery(hql);
query.setComment("My HQL: " + hql);
List results = query.list();
The output in your log will have the comment in a Java-style comment before the SQL:
Hibernate: /My HQL: from Supplier/ select supplier0_.id as id, supplier0_.name ➥ as name2_ from Supplier supplier0_
Here is the link that explains how to store Logs into DB. How to sanitize log messages in Log4j to save them in database
Upvotes: 1