Who is running that horrible Hibernate query? Find out with comments
The Dreaded Search Function
I’ve worked at several jobs where the users have asked for a general-purpose search function in their application. It’s the sort of thing where people want the ability to search on, e.g., all customers with a last name that starts with SM, or all of the customers with a balance greater than 1000.00, etc.
If you can resist the request to build such a thing, then by all means, don’t implement it. End-users have a knack for creating queries which will bring your database to its knees, no matter how clever you think you are with indexing or UI design.
If you must do it, you’ll want to keep a close eye on how its used from the outset for when the inevitable nightmare query is run. One way to do this is by using Hibernate’s comments feature.
Use Comments to Hunt them Down
My colleague Matt Brock implemented something like this where we work. Suppose you have a general method for creating an HQL query in a DAO class, the method could look like this:
protected Query createQuery(String hql) { return getSession().createQuery(hql).setComment(getUsername()); }
Next, make sure Hibernate’s SQL logging and comment logging is enabled in your hibernate.hbm.xml (warning – this log can get pretty huge):
<hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="use_sql_comments">true</property> . . .
Your log will now contain the users who are executing each query, like this:
Hibernate: /* johndoe */ select customer0_.customer_id as col_0_0_, customer0_.cust_company_name from CUSTOMER customer0_ where (customer0_.customer_id=12136 or customer0_.customer_id=16884 or customer0_.customer_id=11150 or customer0_.customer_id=155 or customer0_.customer_id
=27265 or customer0_.customer_id=697 or customer0_.customer_id=4133 or customer0_.customer_id=248 or customer0_.customer_id=2550 or customer0_.customer_id=8449)
One caveat: this may not work with stored procedures – SQL Server, in particular, will get nasty if you try to execute a stored procedure with Hibernate comments attached to it.
Happy Hibernating!


June 13th, 2008 at 2:23 pm
Excellent idea! But, I think it might be worthwhile to take in even further and, on all queries, include the class and method name of the code doing the query. I say this because of my experience on an application with poor performance due to way too many queries. I stepped through each query in the query log and determined what the query was doing and if it was really necessary. This was tedious work made even more tedious by the difficulty in finding out what code was producing the query.
June 17th, 2008 at 2:42 pm
The PHP code that Ken posts in his blog is normal code that contains functions that are in the framework he has created http://www.andromeda-project.org. SO you cant get the full effect of the PHP side of things from some of his code.
June 24th, 2008 at 11:22 am
why do not use atributtes types for mapping object product otherwise productid?
July 16th, 2008 at 4:41 am
Hi
It is a great and nice post and I like it.