How to solve javax.jcr.RepositoryException: Query contains more than one selector | AEM

Issue

If you run the following query, it will throw RepositoryException from getNodes().

Sample code:

Session session = resourceResolver.adaptTo(Session.class);
QueryManager queryManager = session.getWorkspace().getQueryManager();

String queryString = "SELECT child.*, page.[cq:lastReplicated] FROM [cq:PageContent] AS page "
              + "INNER JOIN [nt:unstructured] AS child ON ISCHILDNODE(child,page) "
              + "WHERE ISDESCENDANTNODE(page,'/content/geometrixx/en/') ";

Query query = queryManager.createQuery(queryString, Query.JCR_SQL2);
QueryResult result = query.execute();

NodeIterator nodes = result.getNodes();

Exception:

javax.jcr.RepositoryException: Query contains more than one selector: [child, page]
 at org.apache.jackrabbit.oak.jcr.query.QueryResultImpl.getNodes(QueryResultImpl.java:161)

API Reference:
https://docs.adobe.com/content/docs/en/spec/javax.jcr/javadocs/jcr-2.0/javax/jcr/query/QueryResult.html#getNodes()
RepositoryException - if the query contains more than one selector

Environment

AEM

Cause

As the query contains more than one selector, you need to specify the selector (e.g. child or page).

Resolution

Use the getRows method instead of getNodes.

Query query = queryManager.createQuery(queryString, Query.JCR_SQL2);
QueryResult result = query.execute();

RowIterator rows = result.getRows();
while (rows.hasNext()) {
    Row row = rows.nextRow();
    Node node = row.getNode("page");
}

Additional information

Getting repository exception on executing query (inner join) in CQ5.6
http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__4rih-hi_i_am_running.html

 Adobe

Få hjälp snabbare och enklare

Ny användare?