Ananasi wrote:
You have the right debug configuration to capture the SQL statements. You need to define another appender for log4j if you want those statements to go somewhere else as well.
e.g. this properties file will log all messages to console and the org.hibernate.SQL package to both console and a file (sql.log) (I think, little rusty in log4j)
Code:
log4j.rootLogger=INFO, C1
#C1 console appender
log4j.appender.C1=org.apache.log4j.ConsoleAppender
log4j.appender.C1.layout=org.apache.log4j.PatternLayout
log4j.appender.C1.layout.ConversionPattern=[%d] %c{1} %-5p - %m%n
#R1 rolling file appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=sql.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
#Class overrides
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG,C1,R1
Thanks for the reply,
In either case I can't seem to get hibernate to log the sql to anything other than the console. Even if I could, this solution would only be useful if Query.setComment also worked, but even when I set a big obvious string as the comment, it is definately not among all the SQL being written to the console.
And even if setComment worked that still means I'm logging these hundreds of SQL statments to a log and searching the log for that comment. That's kind of a hacked solution. Is there really no direct API that would allow me to simply translate a particular HQL query to SQL? I'm new to hibernate, but not to coding, and I can't believe that this is how everyone debugs their HQL.
So you have a query producing the wrong results. You have a log with a ton of queries, one of which is the bad one. I have to believe that everyone here isn't pouring through giant logs every time they want to find and debug their query.
Sorry. you can tell I'm a bit frustrated.