@Target(value=TYPE) @Retention(value=RUNTIME) public @interface Entity
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:
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.
setDescription
), with a single
parameter that has the same type as the property (the return type does not matter).
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).
Modifier and Type | Optional Element and Description |
---|---|
String |
defaultKeyspace
Specifies a default keyspace to use when doing operations on this entity.
|
public abstract String defaultKeyspace
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.productThe 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.productIf you want the name to be case-sensitive, it must be enclosed in double-quotes, for example:
@Entity(defaultKeyspace = "\"defaultKs\"")
Copyright © 2017–2020. All rights reserved.