DEV Community

Cover image for JNoSQL CriteriaQuery API preview
amoscatelli
amoscatelli

Posted on

JNoSQL CriteriaQuery API preview

Have you ever heard of Jakarta NoSQL ?

Since JavaEE adoption and rebranding by Eclipse Foundation, I consider it the most exciting feature for the upcoming Jakarta EE 10.

By splitting the NoSQL databases in four different abstractions (Column, Key-Value, Graph and Document), it allows agnostic coding with such technologies. So, for example, you could access both MongoDB and ElasticSearch with the same codebase, for they are both categorized and exposed as Document databases.

Cool, isn't it ?

Personally, I already adopted it even if both specification and default implementation, JNoSQL, are in preview state and may lack some functionalities.

For example, I found myself struggling with the absence of a proper Criteria API, allowing us to build up a criteria query object programmatically, where we could apply different kinds of filtration rules and logical conditions.

Also, following the JPA example, I would expect such Criteria API to be used in combination with an automatically generated Metamodel attributes.

Typos and backend don't get on well, do they ?

So I decided to contribute myself and, with the support of Otavio Santana, JNoSql Lead, this functionality has been delivered in the brand-new 1.0.0-b4 version.

Looking for a concrete example ?
Here you are :

CriteriaQuery<Person> personQuery = template.createQuery(Person.class);

EntityQueryResult<Person> executeQuery = template.executeQuery(
        personQuery.select().where(
                personQuery.from().get(
                        Person_.name
                ).equal(
                        "Poliana"
                ).or(
                        personQuery.from().get(
                                Person_.age
                        ).greaterThanOrEqualTo(17)
                )
        )
);

Stream<Person> stream = executeQuery.getEntities();
Enter fullscreen mode Exit fullscreen mode

For the moment, it has been published as a MongoDB extension but, with the community approval and once it's stable enough, it will brought to the main API and will work with every other Document NoSQL databases.

There are still many features to add and possibilities to explore.
For example, I strongly believe that Criteria API should also support update and delete operations too.
Also, wouldn't it be nice to have this working with other NoSQL abstractions (Column) ?

What do you think ?

Top comments (0)