如何从JPQLQuery获取SQL字符串

编程入门 行业动态 更新时间:2024-10-22 19:27:05
本文介绍了如何从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字符串

本文发布于:2023-10-27 00:04:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   JPQLQuery   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!