Annotation Type Select
-
@Target(METHOD) @Retention(RUNTIME) public @interface Select
Annotates aDaomethod that selects one or more rows, and maps them to instances of anEntity-annotated class.Example:
@Dao public interface ProductDao { @Select Product findById(UUID productId); }Parameters
IfcustomWhereClause()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 thePartitionKeyandClusteringColumnannotations 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 withlimit(),perPartitionLimit(),orderBy(),groupBy(),allowFiltering()andbypassCache(). 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 withCqlName. 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>orUnaryOperator<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.Return type
In all cases, the method can return:
- the entity class itself. If the query returns no rows, the method will return
null. If it returns more than one row, subsequent rows will be discarded.@Select Product findById(UUID productId); - an
Optionalof the entity class. If the query returns no rows, the method will returnOptional.empty(). If it returns more than one row, subsequent rows will be discarded.@Select Optional<Product> findById(UUID productId); - a
PagingIterableof 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); - a
CompletionStageorCompletableFutureof any of the above. The method will execute the query asynchronously. Note that for iterables, you need to switch to the asynchronous equivalentMappedAsyncPagingIterable.@Select CompletionStage<Product> findByIdAsync(UUID productId); @Select CompletionStage<Optional<Product>> findByIdAsync(UUID productId); @Select(customWhereClause = "description LIKE :searchString") CompletionStage<MappedAsyncPagingIterable<Product>> findByDescriptionAsync(String searchString); - a
MappedReactiveResultSetof the entity class.@Select(customWhereClause = "description LIKE :searchString") MappedReactiveResultSet<Product> findByDescriptionReactive(String searchString); - a custom type.
Target keyspace and table
If a keyspace was specified when creating the DAO (seeDaoFactory), then the generated query targets that keyspace. Otherwise, it doesn't specify a keyspace, and will only work if the mapper was built from aSessionthat 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).
- the entity class itself. If the query returns no rows, the method will return
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description booleanallowFilteringWhether to add an ALLOW FILTERING clause to the SELECT query.booleanbypassCacheWhether to add BYPASS CACHE clause to the SELECT query.StringcustomWhereClauseA custom WHERE clause for the SELECT query.String[]groupByA list of column names to be added to a GROUP BY clause in the SELECT query.StringlimitThe LIMIT to use in the SELECT query.String[]orderByA list of orderings to add to an ORDER BY clause in the SELECT query.StringperPartitionLimitThe PER PARTITION LIMIT to use in the SELECT query.StringusingTimeoutThe timeout to use in the generated SELECT query.
-
-
-
Element Detail
-
customWhereClause
String customWhereClause
A custom WHERE clause for the SELECT query.If this is not empty, it completely replaces the WHERE clause in the generated query. Note that the provided string must not contain the
WHEREkeyword.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.
- Default:
- ""
-
-
-
limit
String limit
The LIMIT to use in the SELECT query.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.- Default:
- ""
-
-
-
perPartitionLimit
String perPartitionLimit
The PER PARTITION LIMIT to use in the SELECT query.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.- Default:
- ""
-
-
-
orderBy
String[] orderBy
A list of orderings to add to an ORDER BY clause in the SELECT query.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.
- Default:
- {}
-
-
-
groupBy
String[] groupBy
A list of column names to be added to a GROUP BY clause in the SELECT query.- Default:
- {}
-
-
-
usingTimeout
String usingTimeout
The timeout to use in the generated SELECT query. Equivalent toUSING TIMEOUT <duration>clause.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 String representing a valid CqlDuration.
If the placeholder name is invalid or the literal can't be parsed as a CqlDuration (according to the rules of
CqlDuration.from(String)), the mapper will issue a compile-time error.- Default:
- ""
-
-