开发者

JPA: how do you get/print the JPQL query string behind a (typed) query after parameters have been set?

How do you get/print the JPQL query string behind a (typed) query, that is after parameters have been set? (e.g. for debugging purposes)

A simple toStri开发者_如何学Gong() doesn't seem to do the trick...

Thanks


There is no such thing as "the final JPQL that ultimately gets translated to the final SQL". How a JPA implementation generates the SQL is down to it, and parameters in general will never be substituted into any String. SQL is generated from expression trees etc not a String. If you want param values inserting in then do it yourself since it only makes sense to you


I know this is old, but the current answer doesn't exactly answer the original/root question.

I believe Kawu was looking for what the JPQL string looked like, not if it gets converted to SQL or not. The desire was the find the concrete parameter.

Anyway Kawu, I was looking for the same thing at one time because my query results were skewed and I couldn't tell what the parameter was. I found that adding the below code to the persistence.xml did the trick. I use EclipseLink, but I am sure other JPA implementaitons have something like this:

<property name="eclipselink.logging.level" value="FINE"/>

This shows the below in your server logs:

[EL Fine]: sql: 2016-10-24 16:02:08.577--ServerSession(13483501)--Connection(6214343)--Thread(Thread[27010968@qtp-10395070-0,5,main])--SELECT ID, Name FROM Test WHERE (ID = ?)
    bind => [<your parameter shows here>]


For JBoss users: make the change in standalone.xml. In that file you will find a logging section under <profile>, eg:

    <subsystem xmlns="urn:jboss:domain:logging:3.0">
        <console-handler name="CONSOLE">
            <level name="INFO"/>
            <formatter>
                <named-formatter name="COLOR-PATTERN"/>
            </formatter>
        </console-handler>
        <logger category="com.arjuna">
            <level name="WARN"/>
        </logger>
        <logger category="org.jboss.as.config">
            <level name="DEBUG"/>
        </logger>
        <logger category="sun.rmi">
            <level name="WARN"/>
        </logger>
        <root-logger>
            <level name="INFO"/>
            <handlers>
                <handler name="CONSOLE"/>
            </handlers>
        </root-logger>
        <formatter name="PATTERN">

Default level is INFO. To get queries etc. to be shown, change the level to DEBUG in two places, as in:

    <subsystem xmlns="urn:jboss:domain:logging:3.0">
        <console-handler name="CONSOLE">
            <level name="DEBUG"/>
            <formatter>
                <named-formatter name="COLOR-PATTERN"/>
            </formatter>
        </console-handler>
        <logger category="com.arjuna">
            <level name="WARN"/>
        </logger>
        <logger category="org.jboss.as.config">
            <level name="DEBUG"/>
        </logger>
        <logger category="sun.rmi">
            <level name="WARN"/>
        </logger>
        <root-logger>
            <level name="DEBUG"/>
            <handlers>
                <handler name="CONSOLE"/>
            </handlers>
        </root-logger>
        <formatter name="PATTERN">

Re-start JBoss and now your console is filled with chatter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜