简体   繁体   English

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

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

Maybe I am missing something, but I simply want to (in my java program) get the query string from a javax.persistence.Query object?也许我遗漏了一些东西,但我只是想(在我的 java 程序中)从javax.persistence.Query object 获取查询字符串? The Query object itself doesn't seem to have a method to do that. Query object 本身似乎没有办法做到这一点。 Also I know that our manager doesn't want us to use Spring framework stuff (for example using their QueryUtils class).我也知道我们的经理不希望我们使用 Spring 框架的东西(例如使用他们的QueryUtils类)。

Is there not a way to simply get the query string from javax.persistence.Query object (Again, in a java program)?!有没有办法简单地从javax.persistence.Query object 获取查询字符串(同样,在 java 程序中)?!

There is no JPA-standard way, but some implementations have their own methods. 没有JPA标准方法,但有些实现有自己的方法。 For example DataNucleus JPA allows you to do 例如,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/

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

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();

or open JPA 或者打开JPA

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

...you get the idea.... ......你明白了......

Using OpenJPA I was able to do this:使用 OpenJPA 我能够做到这一点:

OpenJPAQuery<MyEntity> q = (OpenJPAQuery<MyEntity>) query;
String mySQL = q.getQueryString();

The cast to OpenJPAQuery is important because the Query interface does not have the method getQueryString() on it.转换为 OpenJPAQuery 很重要,因为 Query 接口上没有 getQueryString() 方法。

The stored parameters are not filled in unfortunately but I'm working on figuring out how to do that myself.不幸的是,存储的参数没有填写,但我正在努力弄清楚如何自己做到这一点。

Edit your persistence properties file if JPA is provided by hibernate. 如果JPA由hibernate提供,请编辑持久性属性文件。 Add the below section - 添加以下部分 -

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

暂无
暂无

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

相关问题 具有泛型的javax.persistence.Query - javax.persistence.Query with Generics javax.persistence.Query没有设置参数 - javax.persistence.Query does not set parameter 如何模拟和单元测试 javax.persistence.EntityManager 和 javax.persistence.Query? 获取空指针异常 - How to mock and unit test javax.persistence.EntityManager and javax.persistence.Query? Getting NullPointerException 从javax.persistence.Query构建org.eclipse.persistence.queries.ReportQuery - build a org.eclipse.persistence.queries.ReportQuery from a javax.persistence.Query Spring + javax.persistence.Query:管理从H2和PosgreSQL DB本机日期类型到OffsetDateTime的类型转换 - Spring + javax.persistence.Query: Manage type conversion from H2 and PosgreSQL DB native date types to OffsetDateTime hibernate错误:java.lang.ClassCastException:org.hibernate.impl.QueryImpl无法转换为javax.persistence.Query - hibernate Error : java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to javax.persistence.Query 更新/删除查询中的javax.persistence.TransactionRequiredException - javax.persistence.TransactionRequiredException in update/delete query javax.persistence.NoResultException: 未找到要查询的实体 - javax.persistence.NoResultException: No entity found for query JpaRepository 中的本机 @Query 返回一个 Map—我得到 javax.persistence.NonUniqueResultException - Native @Query in JpaRepository returning a Map—I get javax.persistence.NonUniqueResultException SQL(和 Javax.Persistence.Query.ExecuteQuery)如何使用 UPDATE 与 WHERE-IN 和 AND - SQL (and Javax.Persistence.Query.ExecuteQuery) How to use UPDATE with WHERE-IN and AND
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM