Query API

Qi4j sports a new innovative Query API, which uses the domain model and business interfaces to express queries in refactoring-safe Java and no text expressions.
The matter of fact is that we use Java to make the Query to look like Java was involved and evaluating every item, but it is in reality a Domain Specific Language without defining any new language elements, just a set of constraints, methods and algorithms to capture the Java intent, which an underlying Query/Indexing Extension can translate into a native query language, such as SQL or SPARQL.

So how does this look like in reality. Let's look at a simple example;

QueryBuilderFactory factory = ...;
QueryBuilder<Person> builder = factory.newQueryBuilder( Person.class );

Person template  = QueryExpressions.templateFor( Person.class );
builder = builder.where( eq( template.name(), "Niclas" ) );

Query<Person> query = builder.newQuery();
query.setMaxResults( 10 );

for( Person person : query )
{
    // Process each of the maximum 10 person objects
    // in the result set.
}

It is important to notice that the QueryBuilder is immutable and that a new instance is returned on methods that modifies the query composition. This ensures that QueryBuilders being passed around in the system are not accidentally modified, creating hard to trace problems.

Fluent API


The Query API is so called a Fluent API, as it is intended to be written in long sequences of statements, expressing the intent. Operators are obtained from the QueryExpressions static method factory class, as is the templates for the Mixin types and query variables.

Named Native Queries


It is also possible to use the query language of indexing engine directly, in case the Fluent API is incapable of expressing what one is seeking. Such queries are called Named Queries, and must be declared during the assembly phase, to ensure that the codebase is not littered with query strings.

To be able to write native queries, one must also understand how the indexing engine is organizing the data, which is implementation dependent.


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.