Annotation Type Entity


  • @Target(TYPE)
    @Retention(RUNTIME)
    public @interface Entity
    Annotates a class that will be mapped to a Cassandra table or UDT.

    Example:

     @Entity
     public class Product {
       @PartitionKey private UUID id;
       private String description;
    
       public UUID getId() { return id; }
       public void setId(UUID id) { this.id = id; }
       public String getDescription() { return description; }
       public void setDescription(String description) { this.description = description; }
     }
     
    Entity classes follow the usual "POJO" conventions. Each property will be mapped to a CQL column. In order to detect a property:
    • there must be a getter method that follows the usual naming convention (e.g. getDescription) and has no parameters. The name of the property is obtained by removing the "get" prefix and decapitalizing (description), and the type of the property is the return type of the getter.
    • unless the entity is immutable, there must be a matching setter method (setDescription), with a single parameter that has the same type as the property (the return type does not matter).
    There may also be a matching field (description) that has the same type as the property, but this is not mandatory: a property can have only a getter and a setter (for example if the value is computed, or the field has a different name, or is nested into another field, etc.)

    Properties can be annotated to configure various aspects of the mapping. The annotation can be either on the field, or on the getter (if both are specified, the mapper processor issues a compile-time warning, and the field annotation will be ignored). The available annotations are:

    The class must expose a no-arg constructor that is at least package-private.

    Entities are used as arguments or return types of Dao methods. They can also be nested inside other entities (to map UDT columns).

    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      String defaultKeyspace
      Specifies a default keyspace to use when doing operations on this entity.
    • Element Detail

      • defaultKeyspace

        String defaultKeyspace
        Specifies a default keyspace to use when doing operations on this entity.

        This will be used when you build a DAO without an explicit keyspace parameter:

         @Entity(defaultKeyspace = "inventory")
         public class Product { ... }
        
         @Mapper
         public interface InventoryMapper {
           @DaoFactory
           ProductDao productDao();
        
           @DaoFactory
           ProductDao productDao(@DaoKeyspace String keyspace);
         }
        
         ProductDao productDao = mapper.productDao();
         productDao.insert(product); // inserts into inventory.product
        
         ProductDao productDaoTest = mapper.productDao("test");
         productDaoTest.insert(product); // inserts into test.product
         
        The default keyspace optional: if it is not specified, and you build a DAO without a keyspace, then the session must have a default keyspace (set with SessionBuilder.withKeyspace(String)), otherwise an error will be thrown:
         @Entity
         public class Product { ... }
        
         CqlSession session = CqlSession.builder()
             .withKeyspace("default_ks")
             .build();
         InventoryMapper mapper = new InventoryMapperBuilder(session).build();
        
         ProductDao productDao = mapper.productDao();
         productDao.insert(product); // inserts into default_ks.product
         
        If you want the name to be case-sensitive, it must be enclosed in double-quotes, for example:
         @Entity(defaultKeyspace = "\"defaultKs\"")
         
        Default:
        ""