java 如何从 javax.persistence.Query 获取查询字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13609896/
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-10-31 13:24:09  来源:igfitidea点击:

how to get query string from a javax.persistence.Query?

javajpaormquery-string

提问by user1006375

Maybe I am missing something, but I simply want to (in my java program) get the query string from a javax.persistence.Queryobject? The Queryobject itself doesn't seem to have a method to do that. Also I know that our manager doesn't want us to use Spring framework stuff (for example using their QueryUtilsclass).

也许我遗漏了一些东西,但我只是想(在我的 Java 程序中)从javax.persistence.Query对象中获取查询字符串?该Query对象本身似乎并不具备这样做的方法。我也知道我们的经理不希望我们使用 Spring 框架的东西(例如使用他们的QueryUtils类)。

Is there not a way to simply get the query string from javax.persistence.Queryobject (Again, in a java program) ?!

有没有办法简单地从javax.persistence.Query对象中获取查询字符串(同样,在 Java 程序中)?!

回答by tom

no problem. hibernate:

没问题。休眠:

query.unwrap(org.hibernate.Query.class).getQueryString()

or eclipselink

或 eclipselink

query.unwrap(JpaQuery.class).getDatabaseQuery().getJPQLString(); // doesn't work for CriteriaQuery query.unwrap(JpaQuery.class).getDatabaseQuery().getSQLString();

query.unwrap(JpaQuery.class).getDatabaseQuery().getJPQLString(); // doesn't work for CriteriaQuery query.unwrap(JpaQuery.class).getDatabaseQuery().getSQLString();

or open JPA

或打开 JPA

query.unwrap(org.apache.openjpa.persistence.QueryImpl.class).getQueryString()

...you get the idea....

......你明白了......

回答by DataNucleus

There is no JPA-standard way, but some implementations have their own methods. For example DataNucleus JPA allows you to do

没有 JPA 标准的方法,但一些实现有自己的方法。例如 DataNucleus JPA 允许你做

query.toString();

Look at the docs of your implementation for how they do it. See also this blog entry http://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/

查看您的实现文档,了解它们是如何执行的。另请参阅此博客条目 http://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/

回答by James

There is no standard way in JPA,

JPA中没有标准方式,

If you are using EclipseLink see,

如果您使用的是 EclipseLink,请参阅

http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_get_the_SQL_for_a_Query.3F

http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_get_the_SQL_for_a_Query.3F

回答by Anindya Mukherjee

Edit your persistence properties file if JPA is provided by hibernate. Add the below section -

如果 JPA 由 hibernate 提供,请编辑持久性属性文件。添加以下部分 -

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