@Target(value=METHOD) @Retention(value=RUNTIME) 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. It is also possible for the method to
only take a partial primary key (the first n columns), in which case it will return
multiple entities.
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);The generated SELECT query can be further customized with
limit()
, perPartitionLimit()
, orderBy()
, groupBy()
and allowFiltering()
. Some
of these clauses can also contain placeholders whose values will be provided through additional
method parameters. Note that it is sometimes not possible to determine if a parameter is a
primary key component or a placeholder value; therefore the rule is that if your method takes
a partial primary key, the first parameter that is not a primary key component must be explicitly
annotated with CqlName
. For example if the primary key is ((day int, hour int,
minute int), ts timestamp)
:
// Annotate 'l' so that it's not mistaken for the second PK component @Select(limit = ":l") PagingIterable<Sale> findDailySales(int day, @CqlName("l") int l);
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 |
---|---|
boolean |
allowFiltering
Whether to add an ALLOW FILTERING clause to the SELECT query.
|
String |
customWhereClause
A custom WHERE clause for the SELECT query.
|
String[] |
groupBy
A list of column names to be added to a GROUP BY clause in the SELECT query.
|
String |
limit
The LIMIT to use in the SELECT query.
|
String[] |
orderBy
A list of orderings to add to an ORDER BY clause in the SELECT query.
|
String |
perPartitionLimit
The PER PARTITION LIMIT to use in 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.
public abstract String limit
If this starts with ":", it is interpreted as a named placeholder (that must have a corresponding parameter in the method signature). Otherwise, it must be a literal integer value.
If the placeholder name is invalid or the literal can't be parsed as an integer (according
to the rules of Integer.parseInt(String)
), the mapper will issue a compile-time
warning.
public abstract String perPartitionLimit
If this starts with ":", it is interpreted as a named placeholder (that must have a corresponding parameter in the method signature). Otherwise, it must be a literal integer value.
If the placeholder name is invalid or the literal can't be parsed as an integer (according
to the rules of Integer.parseInt(String)
), the mapper will issue a compile-time
warning.
public abstract String[] orderBy
Each element must be a column name followed by a space and the word "ASC" or "DESC". If there are multiple columns, pass an array:
@Select(orderBy = {"hour DESC", "minute DESC"})
If an element can't be parsed, the mapper will issue a compile-time error.
public abstract String[] groupBy
Copyright © 2017–2019. All rights reserved.