我试图基于jcr为CQ创建一个页面。我想查询包含属性中特定字符串的页面。
查询本身(JCR-SQL2)如下(我在CQ接口上测试了它并交付了结果):
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发布于 2013-09-24 11:11:49
这不是因为您创建了一个迭代器两次:首先
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://stackoverflow.com/questions/18977563
复制相似问题