Thursday, February 05, 2009

How to get Hibernate parameter bindings and ONLY parameter bindings

Let me preface this by saying that Hibernate is a gimpy pile of steaming mule shit.

With that out of the way, if you search around with Google and look for information on how to get the parameter bindings for SQL statements executed by Hibernate (which brilliantly are not given if you turn on SQL logging; see also: "mule shit"), you'll see a lot of smartass answers like setting org.hibernate.types=debug in your log4j configuration.

While that configuration change does include the parameter bindings, it also includes a giant shit-pile (mule) of extra data that you probably couldn't possibly care less about, including every time Hibernate binds a return value to a type. These different messages cannot be immediately differentiated by severity or by type (see also: "gimpy").

There is a mostly-workable solution, provided Hibernate has implemented/continues to implement their debugging messages in a consistent fashion:
    <appender name="SQLparams" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="TRACE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.StringMatchFilter">
<param name="StringToMatch" value=" to parameter: " />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>

<category name="org.hibernate.type">
<priority value="TRACE"/>
<appender-ref ref="SQLparams" />
</category>



This filters the log4j output of org.hibernate.type.*, including only the lines that contain the text " to parameter: ". This will of course also include constants coming back from your database that include the phrase " to parameter: ", but chances are this will usually be good enough, unless or until Hibernate pries its overinflated head out of its ass.

Labels: , , ,


Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]