One way to debug Hibernate is to log the generated sql, another way is by enabling live statistics. By default ColdFusion has live statistics disabled and for good reason. This is not something you want running in production and its only meant for development. The easiest way to get statistics out of the Hibernate engine at runtime is the Session Factory. Fortunately for us ColdFusion has exposed this to us by using the ormGetSessionFactory() method.
If you run the following code you will see all of the different statistics methods exposed to you.
There are statistics for things like
- Global Statistics
- Entity Statistics for a particular entity
- Collection Statistics for a particular collection role
- Query Statistics for SQL & HQL queries
- Second Level Cache Statistics for detail runtime information about a particular region in the optional second-level cache
Finally we could take this and add some debugging output to application by using the on request end method in our application. The hibernate_stats.cfm could have some output with any or all of the statistics methods.

#1 by Marc Esher on 8/8/10 - 6:49 PM
Thinking out loud here: I see a getQueries() method. I wonder if you could create a new CF debugging template that would let you output the queries that hibernate runs, alongside the query output you get in the normal debugging template.
#2 by Dan Vega on 8/8/10 - 6:57 PM
<cfset people = entityLoad("Person")>
<cfdump var="#people#">
It returned an array with the 1st element being
from Person
#3 by todd sharp on 8/8/10 - 8:17 PM
#4 by Marc esher on 8/8/10 - 9:45 PM
#5 by Raymond Camden on 8/9/10 - 7:51 AM
#6 by todd sharp on 8/9/10 - 8:10 AM
#7 by Raymond Camden on 8/9/10 - 8:13 AM
#8 by todd sharp on 8/9/10 - 8:59 AM
#9 by John Whish on 8/10/10 - 3:39 AM
I tend to use hql over the EntityLoad method which means that I can get the generated SQL. Instead of calling the ORMExecuteQuery, I call my own method which includes some debugging stuff which I adapted from a post on the hibernate forums to work with CF.
For example:
function ExecuteHQL( required string hql, struct params=StructNew(), boolean unique=false )
{
// debug flag is set here for simple demo
var debug = CGI.SERVER_NAME == "localhost";
if ( debug )
{
var factory = CreateObject( "java", "org.hibernate.hql.ast.ASTQueryTranslatorFactory" );
var query = factory.createQueryTranslator( hql, hql, Javacast( "null", "" ), ORMGetSessionFactory() );
query.compile( Javacast( "null", "" ), false );
// add the created SQL to the struct
arguments.sql = query.getSQLString();
// normally this would use cflog, cftrace or a logging object like logbox
WriteOutput( SerializeJSON( arguments ) );
// for demo purposes dump it out
WriteDump( arguments );
}
return ORMExecuteQuery( hql, params, unique );
}
result = ExecuteHQL( "from Artist where upper( lastname ) like :prefix", {prefix='D%'}, false );
Maybe I should get around to blogging this...