Interface Query
- All Known Subinterfaces:
-
TypedQuery<T>
Query represents a string-based query execution
interface used in Jakarta NoSQL. It provides a consistent
mechanism for dynamically executing queries using either named or
positional parameters, and supports core query operations such as
SELECT, UPDATE, and DELETE.
This API is based on Jakarta Query and supports the Jakarta Common
Query Language (JCQL), including clauses such as
SELECT, WHERE, ORDER BY,
UPDATE SET, and DELETE FROM.
This class is mutable and therefore not thread-safe. It is intended to be used in a single-threaded or scoped manner per operation or request.
Usage constraints:
-
result(),stream(), andsingleResult()must only be used withSELECTqueries. -
executeUpdate()must only be used withDELETEorUPDATEqueries. -
If the underlying NoSQL provider does not support a feature or
query type, an
UnsupportedOperationExceptionwill be thrown.
Example usage for SELECT:
List<Person> people = template.query("SELECT * FROM Person WHERE active = true").result();
Example usage for DELETE:
template.query("DELETE FROM Person WHERE active = false")
.executeUpdate();
Example usage for UPDATE:
template.query("UPDATE Person SET active = true WHERE name = 'Ada'")
.executeUpdate();
- Since:
- 1.1.0
-
Method Summary
Modifier and TypeMethodDescriptionBinds a positional parameter to the query.Binds a named parameter to the query.voidExecutes a write operation query (such asUPDATEorDELETE).<T> List<T> result()Executes aSELECTquery and returns the result as aList.<T> Optional<T> Executes aSELECTquery and returns a single result wrapped in anOptional.<T> Stream<T> stream()Executes aSELECTquery and returns the result as aStream.
-
Method Details
-
executeUpdate
void executeUpdate()Executes a write operation query (such asUPDATEorDELETE).This method performs non-select operations. If the query is a
SELECT, it will throw anUnsupportedOperationException.Note: Since some NoSQL databases use eventual consistency or append-only write models, the effects of this operation may not be immediately visible. The propagation of the update or delete may vary depending on the database implementation and its consistency guarantees.
If required parameters (named or positional) are not bound, this operation will fail, and the provider may throw an exception.
template.query("DELETE FROM Person WHERE age < :minAge") .bind("minAge", 18) .executeUpdate();- Throws:
-
UnsupportedOperationException- if the query is aSELECT, or the operation is not supported by the provider
-
result
Executes aSELECTquery and returns the result as aList.This method must only be used for queries that begin with
SELECT. If the query is anUPDATEorDELETE, anUnsupportedOperationExceptionwill be thrown.If required parameters are not bound before execution, the query will fail and the provider will raise an exception or error.
List<Person> adults = template.query("SELECT * FROM Person WHERE age >= :minAge") .bind("minAge", 18) .result();- Type Parameters:
-
T- the type of the entity - Returns:
- list of results or an empty list
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT
-
stream
Executes aSELECTquery and returns the result as aStream.This method must only be used for queries that begin with
SELECT. If the query is anUPDATEorDELETE, anUnsupportedOperationExceptionwill be thrown.If required parameters are not bound before execution, the query will fail and the provider will raise an exception or error.
Stream<Person> stream = template.query("SELECT * FROM Person WHERE active = true") .stream();- Type Parameters:
-
T- the type of the entity - Returns:
- a stream of results
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT
-
singleResult
Executes aSELECTquery and returns a single result wrapped in anOptional.If no result is found, returns
Optional.empty(). If more than one result is found, the behavior is provider-specific and may throw an exception.This method must only be used for queries that begin with
SELECT. If the query is anUPDATEorDELETE, anUnsupportedOperationExceptionwill be thrown.If required parameters are not bound before execution, the query will fail and the provider will raise an exception or error.
Optional<Person> person = template.query("SELECT * FROM Person WHERE id = :id") .bind("id", "p-001") .singleResult();- Type Parameters:
-
T- the type of the entity - Returns:
- optional result
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT - Since:
- 1.1.0
-
bind
Binds a named parameter to the query.Query query = template.query("SELECT * FROM Book WHERE title = :title") .bind("title", "Effective Java");- Parameters:
-
name- the parameter name (without:) value- the value to bind- Returns:
- this query instance for fluent chaining
- Throws:
-
NullPointerException- if the name is null
-
bind
Binds a positional parameter to the query. Positions are 1-based.Query query = template.query("SELECT * FROM Person WHERE age > ?1") .bind(1, 30);- Parameters:
-
position- the parameter position (starting at 1) value- the value to bind- Returns:
- this query instance for fluent chaining
- Throws:
-
IllegalArgumentException- if position is less than 1
-