Named Native Query

If the Fluent Query API is not capable of expressing the query that you need, and you can figure out some way to get around this, it is also possible to write the query in the native query language used by the indexing extension that you intend to use. This means that the queries can be expressed to the full extension of the language, but it also means that you will not be able to change the indexing subsystem without redoing some work.

To be able to write native queries, you will also need to know how the indexing extension organizes the content. It is not organized in the domain structure, as SQL does it, but in a query friendly way for optimal query performance. Typically it is not needed for people to "view" the index store data directly, so no attempt is made toward that goal.

How to write a native query?


The query is typically written in text format, but specialized indexing extensions may provide other means. What is important is that the queries are created during the assembly phase, and can not be created on the fly while the application is running. This constraint has been put in place for your own safety, to ensure that the application code doesn't become littered with extension dependent code.

The queries are created and stored in a NamedQueries instance, which is assigned as Metainfo to the NamedEntityFinder. It could look like this;

public void assemble( ModuleAssembly module )
{
    NamedQueries namedQueries = new NamedQueries();
    namedQueries.addQuery(
        new NamedSparqlDescriptor( "findDogOwners", QUERY_DOGOWNERS )
    );

     :

    new RdfNativeSesameStoreAssembler( namedQueries ).assemble( module );
}

The QUERY_DOGOWNERS is "just" a SPARQL query expressed as a Java string (leaving out the details of that for now).

The RdfNativeSesameStoreAssembler is directly supporting Named Native Queries, but what happens under the hood is really this;

module
    .addServices( RdfIndexingEngineService.class )
    .setMetaInfo( namedQueries )

Executing a Named Query


Named queries then needs to be obtained in the application code. The method for that is found in the QueryBuilderFactory and is called newNamedQuery(). It takes three argument, the entity type to be found, the name of the query as specified in the meta info and a UnitOfWork to use. Named Native Queries can not be used for in-memory querying of iterables as the Fluent Query API can.
WORK IN-PROGRESS

Qi4j and the Qi4j logo are trademarks of Richard Öberg, Niclas Hedhman and the members of the Qi4j Core Team. See Qi4j licensing for more information.
Powered by SiteVisionexternal link.