如何从JPQLQuery获取SQL字符串 [英] How to get the SQL String From JPQLQuery

查看:205
本文介绍了如何从JPQLQuery获取SQL字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EclipseLink。

I'm using EclipseLink.

我有一个JPQLquery,我想获取sql字符串。.
现在我正在做方式:

I've a JPQLquery and I want to get the sql String.. Now I'm doing in this way:

EJBQueryImpl qi = (EJBQueryImpl)jpqlQuery;
String sqlQueryString = qi.getDatabaseQuery().getSQLString();

问题在于,在sqlQueryString中,常量被替换为?

The problem is that in the sqlQueryString the constant are replaced with ?

我尝试获取导航表达式树的值( getSelectionCriteria() getHavingCriteria()),但我以这种方式丢失了类型...

I've tried to get the values navigating the expressions trees (getSelectionCriteria() and getHavingCriteria()) but in this way I loose the type...

有人遇到这样的问题吗?

Do any one ever have a problem like this one?

推荐答案

引自 EclipseLink常见问题解答


要查看JPA查询的SQL,可以
启用FINE或更低版本的登录。

To see the SQL for a JPA Query you can enable logging on FINE or lower.

要在
运行时获取特定查询的SQL,可以使用DatabaseQuery
API。

To get the SQL for a specific Query at runtime you can use the DatabaseQuery API.



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




此SQL将包含?对于
参数。要使用参数将SQL转换为
,您需要一个带有参数
值的
DatabaseRecord。

This SQL will contain ? for parameters. To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.



Session session = em.unwrap(JpaEntityManager.class).getActiveSession();
DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery();
String sqlString = databaseQuery.getTranslatedSQLString(session, recordWithValues);

这篇关于如何从JPQLQuery获取SQL字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆