简体   繁体   English

如何从TopLink查询中获取前n行?

[英]How can I fetch first n rows from a TopLink query?

For optimization purpose, I want to fetch first N results in a subquery (I'm getting first N ID values) and in the main query fetch full rows for the ID values in the subquery and order them. 出于优化目的,我想在子查询中获取前N个结果(我正在获取第一个N个ID值),而在主查询中,要在子查询中获取ID值的完整行并对其进行排序。 What I have now is 我现在所拥有的是

// This just adds params to the subquery
Expression managedEmp = generateExpression(p_upravljackaFilter);
ReportQuery subQuery = new ReportQuery(CustomDocument.class,
    managedEmp);

subQuery.addAttribute("m_id");

Expression exp = new ExpressionBuilder().get("m_id").in(subQuery);
ReadAllQuery testQuery = new ReadAllQuery(CustomDocument.class,
    exp);
testQuery.addAscendingOrdering("m_creationDate");

List documentList = (List)getTopLinkTemplate().executeQuery(testQuery, true);

What I'm trying so far is using a user defined function, like this: 到目前为止,我正在尝试使用用户定义的函数,如下所示:

ExpressionOperator fetchFirst = new ExpressionOperator();
fetchFirst.setSelector(1);
Vector s = new Vector();
s.addElement("FETCH FIRST 5 ROWS ONLY");
fetchFirst.printsAs(s);
fetchFirst.bePostfix();
fetchFirst.setNodeClass(FunctionExpression.class);
ExpressionOperator.initializeOperators();
ExpressionOperator.addOperator(fetchFirst);
expression = expression.and(builder.get("m_datumKreiranja").getFunction(fetchFirst);

This is literally where I stopped so this won't work but it can show you which way I'm heading. 从字面上看,这是我停下来的地方,因此无法正常工作,但可以向您显示我的前进方向。 Is something like this even possible? 这样的事情可能吗? I'm using Java 1.4 and toplink 10g. 我正在使用Java 1.4和toplink 10g。

Really simple, just insert into second line: 真的很简单,只需插入第二行:

managedEmp = managedEmp.postfixSQL("FETCH FIRST 5 ROWS ONLY");

My mistake was in the fact that I tried it like this: 我的错误在于我这样尝试:

managedEmp.postfixSQL("FETCH FIRST 5 ROWS ONLY"); managedEmp.postfixSQL(“仅优先获取5行”);

because I didn't read what postfixSQL does. 因为我没有阅读postfixSQL的内容。

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

相关问题 如何使用getTranslatedSQLString获取Toplink生成的查询? - How can I get Toplink generated query by using getTranslatedSQLString? 如何在 TopLink 中获取 Count 查询的结果? - How can I get the result of a Count query in TopLink? 我如何使用此休眠查询获取行? - How i can fetch the rows using this hibernate query? 使用Java持久性查询语言在子查询中仅获取前n行 - FETCH FIRST n ROWS ONLY within subquery using Java Persistence Query Language OFFSET N FETCH FIRST M ROWS与JDBC和PostgreSQL无法正常工作 - OFFSET N FETCH FIRST M ROWS with JDBC and PostgreSQL not working Java / DB2 FETCH FIRST:n带有自定义参数的ROWS - Java/DB2 FETCH FIRST :n ROWS with custom parameter 如何在Hibernate,Spring,JSP中从db中获取多行? - How can I fetch multiple rows from db in Hibernate , Spring, JSP? 如何使用 JPA 和 Hibernate 更新前 N 行 - How to update the first N rows with JPA and Hibernate 当输入查询只是该数据的一部分时,如何使用 Room 从我的数据库中获取数据? - How can I fetch data from my Database using Room when the input query is only part of that data? 如何从对行数进行计数的SQL查询正确地实现Hibernate SQL查询? - How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM