我可以从 JPA 查询对象中获取 SQL 字符串吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6276122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:53:36  来源:igfitidea点击:

Can I get the SQL string from a JPA query object?

sqljpa

提问by Li Ho Yin

May I know how can I get the sql from a JPA query? or let's say, convert the JPA query to a SQL string? Thank you very much!

我可以知道如何从 JPA 查询中获取 sql 吗?或者让我们说,将 JPA 查询转换为 SQL 字符串?非常感谢!

采纳答案by Matt Handy

If you only want to know how your JPQL or Criteria Query gets translated to the SQL dialect of your database you can enable fine grained logging in the persistence xmland then look into your log files.

如果您只想知道 JPQL 或 Criteria Query 如何转换为数据库的 SQL 方言,您可以在持久性 xml 中启用细粒度日志记录,然后查看您的日志文件。

The property name and value depends on your JPA implementation. Here is an example of the relevant part of persistence.xmlfor EclipseLink:

属性名称和值取决于您的 JPA 实现。以下是EclipseLink的persistence.xml相关部分的示例:

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

回答by Karol S

For Eclipselink: you can extract the SQL the following way:

对于 Eclipselink:您可以通过以下方式提取 SQL:

query.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString()

It works only afterthe query has been executed.

它仅查询执行后才起作用。

回答by Marc Magon

Following Karol's answer - It is possible to retrieve the SQL before executing the statement in EclipseLink :

按照 Karol 的回答 - 可以在 EclipseLink 中执行语句之前检索 SQL:

Session session = em.unwrap(JpaEntityManager.class).getActiveSession();
DatabaseQuery databaseQuery = query.unwrap(EJBQueryImpl.class).getDatabaseQuery();
databaseQuery.prepareCall(session, new DatabaseRecord());
Record r = databaseQuery.getTranslationRow();
String bound = databaseQuery.getTranslatedSQLString(session, r);
String sqlString = databaseQuery.getSQLString();

To retrieve the SQL String During/After execution it is probably best to do so using persistence properties rather than in-code :

要在执行期间/之后检索 SQL 字符串,最好使用持久性属性而不是代码:

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

回答by FrVaBe

Beside enabling the logging like @Matt Handy mentioned it is also possible to get the SQL String for a specific query with eclipselink at runtime as described here.

除了使像@马特方便的日志中提到,也可以描述获得SQL字符串在运行时使用的EclipseLink特定的查询这里

回答by Art Licis

You are probably interested if there's a way to 'extract' JPQL string (either with placeholders for params, or final JPQL after params are filled-in) out of javax.persistence.Query (one of it's possible subclasses to be more precise),- in this case it's not possible according to JPA specification contract. However, this hypothetically might be possible by JPA implementation (e.g., NamedQueryImpl could have #toJPQLString(), which you could access via casting), but I doubt about that. And even if it's possible I don't think it's a good code performing such manipulations. I would suggest finding another design solutions (and for that you could specify what kind of actual problem do you have). E.g., if you are building your queries dynamically, JPA Criteria API could be used for that, and along with 'building' JPA query, you could maintain your internal data structure reflecting the logic of your query.

如果有一种方法可以从 javax.persistence.Query 中“提取”JPQL 字符串(使用参数的占位符,或填充参数后的最终 JPQL)(其中一个可能的子类更精确),您可能会感兴趣, - 在这种情况下,根据 JPA 规范合同,这是不可能的。然而,这在假设上可能通过 JPA 实现来实现(例如,NamedQueryImpl 可以有 #toJPQLString(),您可以通过强制转换来访问它),但我对此表示怀疑。即使有可能,我也不认为这是执行此类操作的好代码。我建议寻找另一种设计解决方案(为此您可以指定您有什么样的实际问题)。例如,如果您正在动态构建查询,则可以使用 JPA Criteria API 以及“构建”

回答by Edwin Dalorzo

Using Hibernate as a provider you can enable the following properties:

使用 Hibernate 作为提供者,您可以启用以下属性:

hibernate.show_sql  

Write all SQL statements to console. This is an alternative to setting the log category org.hibernate.SQL to debug. (e.g. true | false)

将所有 SQL 语句写入控制台。这是将日志类别 org.hibernate.SQL 设置为调试的替代方法。(例如真|假)

hibernate.format_sql

Pretty print the SQL in the log and console. (e.g. true | false)

在日志和控制台中漂亮地打印 SQL。(例如真|假)

Or, as stated above you can enable logging to the debug level for the logger

或者,如上所述,您可以启用日志记录器的调试级别

org.hibernate.SQL

Log all SQL DML statements as they are executed

在执行时记录所有 SQL DML 语句