Annotation Type Increment


  • @Target(METHOD)
    @Retention(RUNTIME)
    public @interface Increment
    Annotates a Dao method that increments a counter table that is mapped to an Entity-annotated class.

    Example:

     @Entity
     public class Votes {
       @PartitionKey private int articleId;
       private long upVotes;
       private long downVotes;
       ... // constructor(s), getters and setters, etc.
     }
     @Dao
     public interface VotesDao {
       @Increment(entityClass = Votes.class)
       void incrementUpVotes(int articleId, long upVotes);
    
       @Increment(entityClass = Votes.class)
       void incrementDownVotes(int articleId, long downVotes);
    
       @Select
       Votes findById(int articleId);
     }
     

    Parameters

    The entity class must be specified with entityClass().

    The method's parameters must start with the full primary key, in the exact order (as defined by the PartitionKey and ClusteringColumn annotations in the entity class). The parameter names don't necessarily need to match the names of the columns, but the types must match. Unlike other methods like Select or Delete, counter updates cannot operate on a whole partition, they need to target exactly one row; so all the partition key and clustering columns must be specified.

    Then must follow one or more parameters representing counter increments. Their type must be long or Long. The name of the parameter must match the name of the entity property that maps to the counter (that is, the name of the getter without "get" and decapitalized). Alternatively, you may annotate a parameter with CqlName to specify the raw column name directly; in that case, the name of the parameter does not matter:

     @Increment(entityClass = Votes.class)
     void incrementUpVotes(int articleId, @CqlName("up_votes") long foobar);
     
    When you invoke the method, each parameter value is interpreted as a delta that will be applied to the counter. In other words, if you pass 1, the counter will be incremented by 1. Negative values are allowed. If you are using Cassandra 2.2 or above, you can use Long and pass null for some of the parameters, they will be ignored (following NullSavingStrategy.DO_NOT_SET semantics). If you are using Cassandra 2.1, null values will trigger a runtime error.

    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.

    Return type

    The method can return void, a void CompletionStage or CompletableFuture, or a ReactiveResultSet.

    Target keyspace and table

    If a keyspace was specified when creating the DAO (see 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).

    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      Class<?>[] entityClass
      A hint to indicate the entity class that is being targeted.
    • Element Detail

      • entityClass

        Class<?>[] entityClass
        A hint to indicate the entity class that is being targeted. This is mandatory, the mapper will issue a compile error if you leave it unset.

        Note that, for technical reasons, this is an array, but only one element is expected. If you specify more than one class, the mapper processor will generate a compile-time warning, and proceed with the first one.

        Default:
        {}