简体   繁体   English

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

[英]How can I get Toplink generated query by using getTranslatedSQLString?

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() . 此代码的问题在于它不返回TranslatedSQLString,它返回的结果与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?). 现在,在我看到的所有示例代码中,他们要么将行作为一个新对象实例化,要么就不麻烦从引用的位置开始写,但是无论如何,这都不起作用(TopLink版本问题?)。 I'm guessing I need to populate the DatabaseRow object myself, but I can't find any example online. 我猜我需要自己填充DatabaseRow对象,但是我找不到在线示例。

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. 那对我有用。

I didn't manage to find any way to do this by using getTranslatedSQLString. 我没有设法找到任何使用getTranslatedSQLString的方法。 I suppose the DatabaseRow needs to be populated, but I have yet to find the proper way. 我想需要填充DatabaseRow,但是我还没有找到正确的方法。 For now, I'm using "bruteforce" substitution, I "memorize" all of my parameters and do a find/replace on each "?" 现在,我使用“ bruteforce”替换,“存储”所有参数,并对每个“?”进行查找/替换。 sign in the query. 登录查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM