6.6.12 Query Results

Once a query has been defined, it can be executed. The method Query.execute() returns a QueryResult.

The results returned always respect the access restrictions of the current session. In other words if the current session does not have read permissions to a particular item, then that item will not be included in the result set even if it would otherwise constitute a match.

As mentioned, all queries are run against the persistent state of a workspace, pending changes stored in the Session are not searched. However, when an item is accessed from within a QueryResult object, the state of the item returned will obey the same semantics as if it were retrieved using a normal Node.getNode or Node.getProperty, in other words the item state will reflect any pending changes currently stored in the session. As a result, it is possible that a property returned as a match will not reflect the value that caused it to be a match (i.e., its persistent state). Applications can clear the Session (either through save or refresh(false)) before running a query in order to avoid such discrepancies.

The QueryResult is returned in two formats: as a table with property names as the column names and a set of rows of values and as a list of nodes. See 6.6.3 Structure of a Query for details of how the various aspects of these two views are governed by the query. The methods below provide access to the two views:

javax.jcr.query.
QueryResult

String[]

getColumnNames()

Returns an array of all the column names in the table view of this result set.

Throws a RepositoryException if an error occurs.

RowIterator

getRows()

Returns an iterator over the Rows of the result table. If an ORDER BY clause was specified in the query, then the order of the returned rows in the iterator will reflect the order specified in that clause. If no items match, an empty iterator is returned.

Throws a RepositoryException if an error occurs.

NodeIterator

getNodes()

Returns an iterator over all nodes that match the query. If an ORDER BY clause was specified in the query, then the order of the returned nodes in the iterator will reflect the order specified in that clause. If no nodes match, an empty iterator is returned.

Throws a RepositoryException if an error occurs.



javax.jcr.query.
Row

Value[]

getValues()

Returns an array of all the values in the same order as the column names returned by QueryResult.getColumnNames().

Throws a RepositoryException if an error occurs.

Value

getValue(String propertyName)

Returns the value of the indicated property in this Row.

If propertyName is not among the column names of the query result table, an ItemNotFoundException is thrown.

Throws a RepositoryException if another error occurs.