@Target(value=METHOD) @Retention(value=CLASS) public @interface Select
Dao
method that selects one or more rows, and maps them to instances of an
Entity
-annotated class.
Example:
@Dao public interface ProductDao { @Select Product findById(UUID productId); }
customWhereClause()
is empty, the mapper defaults to a selection by primary key
(partition key + clustering columns). The method's parameters must match the types of the primary
key columns, in the exact order (which is defined by the integer values of the PartitionKey
and ClusteringColumn
annotations in the entity class). The parameter names
don't necessarily need to match the names of the columns.
If customWhereClause()
is not empty, it completely replaces the WHERE clause. The
provided string can contain named placeholders. In that case, the method must have a
corresponding parameter for each, with the same name and a compatible Java type:
@Select(customWhereClause = "description LIKE :searchString") PagingIterable<Product> findByDescription(String searchString);
A Function<BoundStatementBuilder, BoundStatementBuilder>
or UnaryOperator<BoundStatementBuilder>
can be added as the last
parameter. It will be applied to the statement before execution. This allows you to customize
certain aspects of the request (page size, timeout, etc) at runtime.
In all cases, the method can return:
null
.
If it returns more than one row, subsequent rows will be discarded.
@Select Product findById(UUID productId);
Optional
of the entity class. If the query returns no rows, the method will
return Optional.empty()
. If it returns more than one row, subsequent rows will be
discarded.
@Select Optional<Product> findById(UUID productId);
PagingIterable
of the entity class. It behaves like a result set, except that
each element is a mapped entity instead of a row.
@Select(customWhereClause = "description LIKE :searchString") PagingIterable<Product> findByDescription(String searchString);
CompletionStage
or CompletableFuture
of any of the above. The method will
execute the query asynchronously. Note that for iterables, you need to switch to the
asynchronous equivalent MappedAsyncPagingIterable
.
@Select CompletionStage<Product> findByIdAsync(UUID productId); @Select CompletionStage<Optional<Product>> findByIdAsync(UUID productId); @Select(customWhereClause = "description LIKE :searchString") CompletionStage<MappedAsyncPagingIterable<Product>> findByDescriptionAsync(String searchString);
DaoFactory
), then the generated
query targets that keyspace. Otherwise, it doesn't specify a keyspace, and will only work if the
mapper was built from a Session
that has a default keyspace set.
If a table was specified when creating the DAO, then the generated query targets that table. Otherwise, it uses the default table name for the entity (which is determined by the name of the entity class and the naming convention).
Modifier and Type | Optional Element and Description |
---|---|
String |
customWhereClause
A custom WHERE clause for the SELECT query.
|
public abstract String customWhereClause
If this is not empty, it completely replaces the WHERE clause in the generated query. Note
that the provided string must not contain the WHERE
keyword.
This clause can contain placeholders that will be bound with the method's parameters; see the top-level javadocs of this class for more explanations.
Copyright © 2017–2019. All rights reserved.