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

时间:2023-02-15 00:28:37

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.

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

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.

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

2 个解决方案

#1


0  

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.

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

#2


0  

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.

这对我有用。

#1


0  

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.

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

#2


0  

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.

这对我有用。