如何使用 getTranslatedSQLString 获取 Toplink 生成的查询?

发布于 2024-12-08 16:52:02 字数 580 浏览 2 评论 0原文

所以我正在做的是创建一个获取 ID 值列表的子查询,然后主查询获取所有必要的值并添加排序。

我所得到的是:

ReportQuery querySub = new ReportQuery(Predmet.class, generatedExpression);
querySub.addAttribute("m_id");

DatabaseRow row = new DatabaseRow();
querySub.prepareCall(getSession(), row);

// This part is the problem
String sql = querySub.getTranslatedSQLString(getSession(), row);

此代码的问题是它不返回 TranslatedSQLString,它返回与 querySub.getSQLString() 相同的结果。现在,在我看到的所有示例代码中,他们要么将 row 实例化为新对象,要么懒得从获得引用的位置进行编写,但无论如何,这都不起作用(TopLink 版本问题?)。我猜我需要自己填充 DatabaseRow 对象,但我在网上找不到任何示例。

So what I'm doing is creating a subquery that gets a list of ID values, then the main query gets all the necessary values and adds ordering.

What I have is this:

ReportQuery querySub = new ReportQuery(Predmet.class, generatedExpression);
querySub.addAttribute("m_id");

DatabaseRow row = new DatabaseRow();
querySub.prepareCall(getSession(), row);

// This part is the problem
String sql = querySub.getTranslatedSQLString(getSession(), row);

The problem with this code is that it doesn't return TranslatedSQLString, it returns the same result as querySub.getSQLString(). Now in all the example code I saw, they either instanced row as a new object or didn't bother to write from where they got the reference but whatever the case, this doesn't work (TopLink version issue?). I'm guessing I need to populate the DatabaseRow object myself, but I can't find any example online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-12-15 16:52:02

我没有找到任何方法来使用 getTranslatedSQLString 来做到这一点。我想 DatabaseRow 需要填充,但我还没有找到正确的方法。现在,我正在使用“暴力”替换,我“记住”所有参数并对每个“?”进行查找/替换登录查询。

I didn't manage to find any way to do this by using getTranslatedSQLString. I suppose the DatabaseRow needs to be populated, but I have yet to find the proper way. For now, I'm using "bruteforce" substitution, I "memorize" all of my parameters and do a find/replace on each "?" sign in the query.

风吹雨成花 2024-12-15 16:52:02

您需要像这样获得会话:

JpaHelper.getDatabaseSession(getEntityManager().getEntityManagerFactory());

您需要的数据库是:

TypedQuery<T> typedQuery = getEntityManager().createQuery(cq);
DatabaseQuery databaseQuery = typedQuery.unwrap(JpaQuery.class).getDatabaseQuery();

所以最后一个例子是:

Session session = JpaHelper.getDatabaseSession(getEntityManager().getEntityManagerFactory());
DatabaseQuery databaseQuery = null;
if(typedQuery instanceof EJBQueryImpl)
    databaseQuery = ((EJBQueryImpl<T>)typedQuery).getDatabaseQuery();
else
    databaseQuery = typedQuery.unwrap(JpaQuery.class).getDatabaseQuery();
sql = databaseQuery.getTranslatedSQLString(session, databaseQuery.getTranslationRow());

这对我有用。

you need get session like this:

JpaHelper.getDatabaseSession(getEntityManager().getEntityManagerFactory());

The Database that you need is:

TypedQuery<T> typedQuery = getEntityManager().createQuery(cq);
DatabaseQuery databaseQuery = typedQuery.unwrap(JpaQuery.class).getDatabaseQuery();

So the final example is:

Session session = JpaHelper.getDatabaseSession(getEntityManager().getEntityManagerFactory());
DatabaseQuery databaseQuery = null;
if(typedQuery instanceof EJBQueryImpl)
    databaseQuery = ((EJBQueryImpl<T>)typedQuery).getDatabaseQuery();
else
    databaseQuery = typedQuery.unwrap(JpaQuery.class).getDatabaseQuery();
sql = databaseQuery.getTranslatedSQLString(session, databaseQuery.getTranslationRow());

That work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文