When a new Query object is first created with QueryManager.createQuery it is a transient query. If the repository is level 2 compliant and supports the node type nt:query, then a transient query can be stored in content by calling Query.storeAsNode(String absPath). This creates an nt:query node at the specified path (a save on the parent of the new node is required to persist the stored query). Retrieving a stored query is done by passing the nt:query node to QueryManager.getQuery(Node node).
Note that the actual query statement stored within a persistent query (that is, the value of the property jcr:statement, for example, “//*[@jcr:primaryType='nt:file']” or “SELECT * FROM nt:base WHERE jcr:primaryType='nt:file'”) is namespace-fragile in that it is stored as a literal string with the namespaces in prefix form. As a result, if the stored query is run in a context where a prefix it references has been remapped, the query will not produce the same result as it would have before the remapping. It is left up to the application to ensure that appropriate mappings are in place (either using temporary Session remapping or persistent NamespaceRegistry changes) when a stored query is executed.