| 1 | ## Definition of mapped classes |
| 2 | |
| 3 | The object mapper is configured by annotations on the mapped classes. |
| 4 | |
| 5 | ### Creating a table entity |
| 6 | |
| 7 | A table entity is a class that will be mapped to a table in Cassandra. |
| 8 | It is annotated with [@Table][table]: |
| 9 | |
| 10 | ``` |
| 11 | CREATE TABLE user (user_id uuid PRIMARY KEY, name text); |
| 12 | ``` |
| 13 | |
| 14 | ```java |
| 15 | @Table(keyspace = "ks", name = "users", |
| 16 | readConsistency = "QUORUM", |
| 17 | writeConsistency = "QUORUM", |
| 18 | caseSensitiveKeyspace = false, |
| 19 | caseSensitiveTable = false) |
| 20 | public static class User { |
| 21 | @PartitionKey |
| 22 | @Column(name = "user_id") |
| 23 | private UUID userId; |
| 24 | private String name; |
| 25 | // ... constructors / getters / setters |
| 26 | } |
| 27 | ``` |
| 28 | |
| 29 | `@Table` takes the following options: |
| 30 | |
| 31 | - `keyspace`: the keyspace for the table. |
| 32 | - `name`: the name of the table in the database. |
| 33 | - `caseSensitiveKeyspace`: whether the keyspace name is [case-sensitive]. |
| 34 | - `caseSensitiveTable`: whether the table name is [case-sensitive]. |
| 35 | - `readConsistency`: the [consistency level] that will be used on each |
| 36 | get operation in the mapper. (if unspecified, it defaults to the |
| 37 | cluster-wide setting) |
| 38 | - `writeConsistency`: the [consistency level] that will be used on each |
| 39 | save, or delete operation in the mapper. (if unspecified, it defaults |
| 40 | to the cluster-wide setting) |
| 41 | |
| 42 | The class must provide a default constructor, and all fields (except the |
| 43 | ones annotated `@Transient`) must have corresponding setters and |
| 44 | getters. |
| 45 | |
| 46 | [table]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Table.html |
| 47 | [case-sensitive]:http://docs.datastax.com/en/cql/3.3/cql/cql_reference/ucase-lcase_r.html |
| 48 | [consistency level]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ConsistencyLevel.html |
| 49 | |
| 50 | #### Column names |
| 51 | |
| 52 | By default, the mapper tries to map each Java property to a |
| 53 | case insensitive column of the same name. If you want to use a different |
| 54 | name, or [case-sensitive] names, use the [@Column][column] annotation on |
| 55 | the field. |
| 56 | |
| 57 | [column]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Column.html |
| 58 | |
| 59 | #### Primary key fields |
| 60 | |
| 61 | [@PartitionKey][pk] and [@ClusteringColumn][cc] are used to indicate the |
| 62 | [partition key][pks] and [clustering columns][pks]. In the case of a |
| 63 | composite partition key or multiple clustering columns, the integer |
| 64 | value indicates the position of the column in the key: |
| 65 | |
| 66 | ``` |
| 67 | CREATE TABLE sales(countryCode text, areaCode text, sales int, |
| 68 | PRIMARY KEY((countryCode, area Code))); |
| 69 | ``` |
| 70 | |
| 71 | ```java |
| 72 | @PartitionKey(0) |
| 73 | private String countryCode; |
| 74 | @PartitionKey(1) |
| 75 | private String areaCode; |
| 76 | ``` |
| 77 | |
| 78 | The order of the indices must match that of the columns in the table |
| 79 | declaration. |
| 80 | |
| 81 | [pk]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/PartitionKey.html |
| 82 | [cc]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/ClusteringColumn.html |
| 83 | [pks]:http://thelastpickle.com/blog/2013/01/11/primary-keys-in-cql.html |
| 84 | |
| 85 | #### Computed fields |
| 86 | |
| 87 | [@Computed][computed] can be used on fields that are the result of a |
| 88 | computation on the Cassandra side. Typically a function call. Native |
| 89 | functions in Cassandra like `writetime()` or [User Defined Functions] are |
| 90 | supported. |
| 91 | |
| 92 | ```java |
| 93 | @Computed("ttl(name)") |
| 94 | Integer ttl; |
| 95 | ``` |
| 96 | |
| 97 | The CQL return type of function must match the type of the field, |
| 98 | otherwise an exception will be thrown. |
| 99 | |
| 100 | Computed fields are ignored when saving an entity. |
| 101 | |
| 102 | `@Computed` does not support case-sensitivity. If the expression |
| 103 | contains case-sensitive column or function names, you'll have to escape |
| 104 | them: |
| 105 | |
| 106 | ```java |
| 107 | @Computed("\"myFunction\"(\"myColumn\")") |
| 108 | int f; |
| 109 | ``` |
| 110 | |
| 111 | Finally, computed fields are only supported with [basic read |
| 112 | operations](../using/#basic-crud-operations) at this time. |
| 113 | Support in [accessors](../using/#accessors) in planned for a future |
| 114 | version (see |
| 115 | [JAVA-832](https://datastax-oss.atlassian.net/browse/JAVA-832)). |
| 116 | |
| 117 | [User Defined Functions]:http://www.planetcassandra.org/blog/user-defined-functions-in-cassandra-3-0/ |
| 118 | [computed]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Computed.html |
| 119 | |
| 120 | #### Transient fields |
| 121 | |
| 122 | [@Transient][transient] can be used to prevent a field from being mapped. |
| 123 | |
| 124 | [transient]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Transient.html |
| 125 | |
| 126 | ### Mapping User Types |
| 127 | |
| 128 | [User Defined Types] can also be mapped by using [@UDT][udt]: |
| 129 | |
| 130 | ``` |
| 131 | CREATE TYPE address (street text, zip_code int); |
| 132 | ``` |
| 133 | |
| 134 | ```java |
| 135 | @UDT(keyspace = "ks", name = "address") |
| 136 | class Address { |
| 137 | private String street; |
| 138 | @Field(name = "zip_code") |
| 139 | private int zipCode; |
| 140 | // ... constructors / getters / setters |
| 141 | } |
| 142 | ``` |
| 143 | |
| 144 | `@UDT` takes the following options: |
| 145 | |
| 146 | - `keyspace`: the keyspace for the UDT. |
| 147 | - `name`: the name of the UDT in the database. |
| 148 | - `caseSensitiveKeyspace`: whether the keyspace name is case-sensitive. |
| 149 | - `caseSensitiveType`: whether the UDT name is case-sensitive. |
| 150 | |
| 151 | The class must provide a default constructor, and all fields (except the |
| 152 | ones annotated `@Transient`) must have corresponding setters and |
| 153 | getters. |
| 154 | |
| 155 | As in table entities, fields are mapped to an UDT field of the same |
| 156 | name by default. To declare a different name or use case-sensitivity, |
| 157 | use the [@Field][field] annotation: |
| 158 | |
| 159 | ```java |
| 160 | @Field(name = "zip_code") |
| 161 | private int zipCode; |
| 162 | ``` |
| 163 | |
| 164 | When a table has a UDT column, the mapper will automatically map it to |
| 165 | the corresponding class: |
| 166 | |
| 167 | ``` |
| 168 | CREATE TABLE company (company_id uuid PRIMARY KEY, name text, address address); |
| 169 | ``` |
| 170 | |
| 171 | ```java |
| 172 | public static class Company { |
| 173 | @PartitionKey |
| 174 | @Column(name = "company_id") |
| 175 | private UUID companyId; |
| 176 | private String name; |
| 177 | private Address address; |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | (this also works with UDTs inside collections or other UDTs, with |
| 182 | arbitrary level of nesting) |
| 183 | |
| 184 | [User Defined Types]: ../../udts/ |
| 185 | [udt]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/UDT.html |
| 186 | [field]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Field.html |
| 187 | |
| 188 | ### Mapping collections |
| 189 | |
| 190 | Java collections will be automatically mapped into corresponding |
| 191 | Cassandra types. As in Cassandra, collections can contain all native |
| 192 | types and all [user types](#mapping-user-types) previously defined is |
| 193 | the database. |
| 194 | |
| 195 | Collection and UDT fields should be annotated to indicate whether they are |
| 196 | frozen. Currently this is only for informational purposes (the mapper |
| 197 | won't check that the declarations match the rules in Cassandra). |
| 198 | However it is a good idea to keep using these annotations and make sure |
| 199 | they match the schema, in anticipation for the schema generation features |
| 200 | that will be added in a future version. |
| 201 | The default annotation is [@Frozen][frozen], [@FrozenKey][frozenkey] and |
| 202 | [@FrozenValue][frozenvalue]` are also provided for convenience: |
| 203 | |
| 204 | ```java |
| 205 | // Will be mapped as a 'list<text>' |
| 206 | private List<String> stringList; |
| 207 | |
| 208 | // Will be mapped as a 'frozen<list<text>>' |
| 209 | @Frozen |
| 210 | private List<String> frozenStringList; |
| 211 | |
| 212 | // Will be mapped as 'map<frozen<address>, frozen<list<text>>>' |
| 213 | @FrozenKey |
| 214 | @FrozenValue |
| 215 | private Map<Address, List<String>> frozenKeyValueMap; |
| 216 | |
| 217 | // Will be mapped as 'map<text, frozen<list<address>>>' |
| 218 | @FrozenValue |
| 219 | private Map<String, List<Address>> frozenValueMap; |
| 220 | ``` |
| 221 | |
| 222 | [frozen]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Frozen.html |
| 223 | [frozenkey]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/FrozenKey.html |
| 224 | [frozenvalue]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/FrozenValue.html |