Canned queries are SQL queries executed on the Alfresco database and not on Solr. So If you want to perform a full-text search or having spelling correction or facets then you’re not in the right place. One main reason to use canned
queries
is that Solr is not transactional, it’s near real time, but if you want to assure having exactly what is in the DB then you will need
canned queries. Behind the scene, Alfresco uses a persistence framework called Ibatis which replaced hibernate some time ago.
Use Case
Getting all nodeRefs for a custom type using a canned query. Since you have thousands nodes of this type you want to be able to use
paging.
Ibatis XML files
custom-sqlMap.xml
This xml file is used to defined the SQL stored procedures, that’s the way Ibatis works. The file is called a mapper.
We can see there is a new namespace declared.
For our case there is only one simple select. It has:
an id.
a parameter type “Node”.
a result Map “result_NodeRef”.
The mapping of the “result_NodeRef” has been defined at the begin of the file.
The parts of the where clause like this #{parameter} are properties taken from the parameter. Node is an alias to a java
class.
custom-sqlMapConfig.xml
This configuration file is used to “reference” the mapper files, we can see our.
Aliases are defined here as well. You can see the parameter type “Node” from our select.
Java classes: canned query and factory
GetCustomNodesCQ.java
This class allows to execute the canned query we wrote in the XML. You have to pass the namespace and the id of the query to the
executeQuery method, and voilà.
There is another abstract class AbstractCannedQueryPermissions you can extend if you want to apply permissions.
GetCustomNodesCQFactory.java
A canned query can be used only once. So each time you want to execute it then you need a new instance. That is the job of the canned
query factory.
Java custom service
CustomCannedQueryService.java
As we can see it is a bit complex, all of these xml files and java classes. However to work better with it you can encapsulate the
call to the factory and the execution of the query in a nice service.
How to use it
This example shows how to use the paging feature. It’s possible to get all nodes in a single page if
“skip” is set to 0 and “maxItemPerPage” to Integer.MAX_VALUE.
Spring context
custom-ibatis-context.xml
End
I hope this was useful. It’s a basic example but at the same time you have now a nice overview of what are canned queries. Also, I
think there is enough vocabulary in this page for you to look for other examples in the Alfresco source code.