1

我正在尝试基于 jcr API 为 CQ 创​​建一个页面。我想查询在属性中包含特定字符串的页面。

查询本身 (JCR-SQL2) 如下(我在 CQ Web 界面上对其进行了测试并提供了结果):

SELECT
    *
FROM 
    [nt:base] AS s
WHERE
    ISDESCENDANTNODE([/content])
AND
    s.[sling:resourceType] = 'some/path/to/destination'

这是我在 JSP 页面中的尝试:

    String stmt = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND s.[cq:template] = 'some/path/to/destination'";
    Query query = currentNode.getSession().getWorkspace().getQueryManager().createQuery(stmt, Query.JCR_SQL2);
    QueryResult results = query.execute();
    if (results.getNodes() != null && results.getNodes().hasNext()) {
        NodeIterator it = results.getNodes();

        while (it.hasNext()) {
            Node node = it.nextNode();
            String npath = node.getPath();
            Page contentPage = pageManager.getContainingPage(resourceResolver.getResource(npath));
            String title = contentPage.getTitle();
            String path = contentPage.getPath() + ".html";
            %>
            <div class="searchresult"><a href="<%= path %>"><%= title %></a></div>
            <%
        }
    }

抛出的错误如下:

Caused by: javax.jcr.RepositoryException: This query result has already been iterated through
4

1 回答 1

4

是不是因为你创建了两次迭代器:第一次

if (results.getNodes() != null && results.getNodes().hasNext()) {

接着

NodeIterator it = results.getNodes();

我会尝试像这样只创建一次:

   QueryResult results = query.execute();
   NodeIterator it = results.getNodes();
   if (it.hasNext()) {
   ...
   }

查看 SimpleQueryResult 的来源,它正在做一些奇怪的事情,实例变量 rowIterator 设置为 null,所以下次调用 getRows 时,您将进入条件的 else 部分,您将遇到您提到的错误:

  public synchronized RowIterator getRows() throws RepositoryException {
        if (rowIterator != null) {
            RowIterator iterator = rowIterator;
            rowIterator = null;
            return iterator;
        } else {
            throw new RepositoryException(
                    "This query result has already been iterated through");
        }
}

https://svn.apache.org/repos/asf/jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/SimpleQueryResult.java

于 2013-09-24T11:11:49.013 回答