简体   繁体   English

JPA:如何在设置参数后获取/打印(类型化)查询后面的JPQL查询字符串?

[英]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? 你如何/打印一个(类型)查询背后的JPQL查询字符串,即参数设定 (eg for debugging purposes) (例如用于调试目的)

A simple toString() doesn't seem to do the trick... 一个简单的toString()似乎没有做到这一点......

Thanks 谢谢

There is no such thing as "the final JPQL that ultimately gets translated to the final SQL". 没有“最终的JPQL最终被转换为最终的SQL”。 How a JPA implementation generates the SQL is down to it, and parameters in general will never be substituted into any String. JPA实现如何生成SQL取决于它,一般参数永远不会被替换为任何String。 SQL is generated from expression trees etc not a String. SQL是从表达式树等生成的,而不是String。 If you want param values inserting in then do it yourself since it only makes sense to you 如果你想插入param值然后自己做,因为它只对你有意义

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. 我相信Kawu正在寻找JPQL字符串的样子,而不是它是否转换为SQL。 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. 无论如何Kawu,我一直在寻找相同的东西,因为我的查询结果是偏斜的,我无法分辨参数是什么。 I found that adding the below code to the persistence.xml did the trick. 我发现将以下代码添加到persistence.xml就可以了。 I use EclipseLink, but I am sure other JPA implementaitons have something like this: 我使用EclipseLink,但我确信其他JPA实现有类似这样的东西:

<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. 对于JBoss用户:在standalone.xml中进行更改。 In that file you will find a logging section under <profile>, eg: 在该文件中,您将在<profile>下找到一个日志记录部分,例如:

    <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. 默认级别为INFO。 To get queries etc. to be shown, change the level to DEBUG in two places, as in: 要显示查询等,请在两个位置将级别更改为DEBUG,如下所示:

    <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. 重新启动JBoss,现在你的控制台充满了喋喋不休。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM