JCR SQL2查询显然没有提供任何结果

时间:2013-09-24 09:22:01

标签: java cq5 jcr aem jcr-sql2

我正在尝试基于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

1 个答案:

答案 0 :(得分: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时你会去条件的其他部分,你会得到你提到的错误:

  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