我正在使用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字符串
发布评论