1
votes

I am having difficulties querying a jcr node in jackrabbit, containing certain property.

I've added a custom node to add mixin properties to it.

<custom = 'http://example.com/mydomain'>
[custom:extensible] mixin
- * (undefined) multiple 
- * (undefined) 

Then I add a nt:file nodes in the following way and to those nt:file I add my custom:extensible

documents = session.getRootNode().getNode(MainJCRConstants.MAIN_NODE);
documentNode = documents.addNode(fileName, NodeType.NT_FILE);
Node resNode = documentNode.addNode(JcrConstants.JCR_CONTENT, NodeType.NT_RESOURCE);
Binary binary = session.getValueFactory().createBinary(ins);
resNode.setProperty(JcrConstants.JCR_DATA, binary);
resNode.addMixin("custom:extensible");
resNode.setProperty("sourceSystem", "internal");

And now I want to query all [nt:file] nodes, containing property "sourceSystem" which value is equal to "internal".

After looking at the documentation I fugred I have to use something similar to:

SELECT parent.* FROM [nt:file] AS parent 
INNER JOIN [custom:extensible] AS child ON ISCHILDNODE(child,parent) 
WHERE CONTAINS(parent.*, 'internal') OR CONTAINS(child.*,'internal')

But this throws an exception http://pastebin.com/ZdxZPf2C

Also every other query I've tried either throws an exception or returns an empty result set.

Solution:

SELECT * FROM [nt:resource] as res INNER JOIN [custom:extensible] AS ext ON ISSAMENODE(res,ext) WHERE CONTAINS(res.sourceSystem,'internal') OR CONTAINS(ext.sourceSystem,'internal')
1

1 Answers

1
votes

You can query a node both by primary type and mixin, and it does require using a JOIN. However, the join criteria should be a ISSAMENODE criteria:

SELECT * FROM [nt:file] AS file
   INNER JOIN [custom:extensible] AS ext ON ISSAMENODE(file,ext)
WHERE CONTAINS(file.*,'internal') OR CONTAINS(ext.*,'internal')

This query joins all nt:file nodes with all custom:extensible nodes, ensuring that the only pairing in the results occur when the nt:file node and custom:extensible nodes are the same node.

Also note that the SELECT * may mean your results contain duplicate columns, so you may consider explicitly selecting the columns your are interested in.