Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capture final SQL query generated by hibernate JPA in JBoss AS 7

I have been trying really hard to capture the exact SQL of a really complicated JPA query in my project for the purpose of optimization but so far i am unable to do so. I have tried the following ways:

1) Tried enabling hibernate logs via logback by putting following in my logback.xml and enabling show_SQL

    <logger name="org.hibernate.type" level="ALL" />
 <logger name="org.hibernate" level="TRACE">
    <appender-ref ref="fdpAdminAppender" />
 </logger>

2) Tried using P6spy-2 drivers with Jboss AS7 and postgres but just couldn't make it working. It gives the following exception.

com.p6spy.engine.spy.P6SpyDriver cannot be cast to javax.sql.XADataSource

3) Enabled inbuild Datasource spy in JBoss AS 7 and it did work. But the logs show way too much data all of which is useless to me as they again append the values as '?'. Even if i can get a way to get complete SQL statements here, my problem will be solved.

So far i have succeeded with step 3 only but either i am missing out something or it works this way, either case my actual purpose is not resolved. So please somebody help me get the actual SQL query from JPA. I am not concerned about how i get it, i just need to get it. I have experienced the need to verify the actual SQL generated by JPA with values so many times during development. It is strange why there is no standard way to do that.

like image 675
ishan Avatar asked Mar 18 '14 09:03

ishan


1 Answers

Just add the following inside the 'persistence.xml':

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

Make sure to put it inside the <properties> tag, just like this:

<persistence>
    <persistence-unit>
        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Good luck :)

like image 63
N0nbot Avatar answered Oct 25 '22 16:10

N0nbot