Elffuss BraidHub

kikocis/java-driver public

6154c03e10b0 · 7.4 KiBRenderedRawPermalink
1## Definition of mapped classes
2
3The object mapper is configured by annotations on the mapped classes.
4
5### Creating a table entity
6
7A table entity is a class that will be mapped to a table in Cassandra.
8It is annotated with [@Table][table]:
9
10```
11CREATE 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)
20public 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
42The class must provide a default constructor, and all fields (except the
43ones annotated `@Transient`) must have corresponding setters and
44getters.
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
52By default, the mapper tries to map each Java property to a
53case insensitive column of the same name. If you want to use a different
54name, or [case-sensitive] names, use the [@Column][column] annotation on
55the 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
63composite partition key or multiple clustering columns, the integer
64value indicates the position of the column in the key:
65
66```
67CREATE TABLE sales(countryCode text, areaCode text, sales int,
68 PRIMARY KEY((countryCode, areaCode)));
69```
70
71```java
72@PartitionKey(0)
73private String countryCode;
74@PartitionKey(1)
75private String areaCode;
76```
77
78The order of the indices must match that of the columns in the table
79declaration.
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
88computation on the Cassandra side. Typically a function call. Native
89functions in Cassandra like `writetime()` or [User Defined Functions] are
90supported.
91
92```java
93@Computed("ttl(name)")
94Integer ttl;
95```
96
97The CQL return type of function must match the type of the field,
98otherwise an exception will be thrown.
99
100Computed fields are ignored when saving an entity.
101
102`@Computed` does not support case-sensitivity. If the expression
103contains case-sensitive column or function names, you'll have to escape
104them:
105
106```java
107@Computed("\"myFunction\"(\"myColumn\")")
108int f;
109```
110
111Finally, computed fields are only supported with [basic read
112operations](../using/#basic-crud-operations) at this time.
113Support in [accessors](../using/#accessors) in planned for a future
114version (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```
131CREATE TYPE address (street text, zip_code int);
132```
133
134```java
135@UDT(keyspace = "ks", name = "address")
136class 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
151The class must provide a default constructor, and all fields (except the
152ones annotated `@Transient`) must have corresponding setters and
153getters.
154
155As in table entities, fields are mapped to an UDT field of the same
156name by default. To declare a different name or use case-sensitivity,
157use the [@Field][field] annotation:
158
159```java
160@Field(name = "zip_code")
161private int zipCode;
162```
163
164When a table has a UDT column, the mapper will automatically map it to
165the corresponding class:
166
167```
168CREATE TABLE company (company_id uuid PRIMARY KEY, name text, address address);
169```
170
171```java
172public 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
182arbitrary 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
190Java collections will be automatically mapped into corresponding
191Cassandra types. As in Cassandra, collections can contain all native
192types and all [user types](#mapping-user-types) previously defined is
193the database.
194
195Collection and UDT fields should be annotated to indicate whether they are
196frozen. Currently this is only for informational purposes (the mapper
197won't check that the declarations match the rules in Cassandra).
198However it is a good idea to keep using these annotations and make sure
199they match the schema, in anticipation for the schema generation features
200that will be added in a future version.
201The 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>'
206private List<String> stringList;
207
208// Will be mapped as a 'frozen<list<text>>'
209@Frozen
210private List<String> frozenStringList;
211
212// Will be mapped as 'map<frozen<address>, frozen<list<text>>>'
213@FrozenKey
214@FrozenValue
215private Map<Address, List<String>> frozenKeyValueMap;
216
217// Will be mapped as 'map<text, frozen<list<address>>>'
218@FrozenValue
219private 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