Elffuss BraidHub

kikocis/java-driver public

c3395b80b593 · 8.2 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/2.1/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/2.1/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/2.1/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, area Code)));
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/2.1/com/datastax/driver/mapping/annotations/PartitionKey.html
82[cc]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/ClusteringColumn.html
83[pks]:http://thelastpickle.com/blog/2013/01/11/primary-keys-in-cql.html
84
85#### Enumerations
86
87[@Enumerated][enum] is used to map Java enumerations. Since Cassandra
88does not support enumerations, the object mapper defines two ways to
89persist an enum value :
90
91- `EnumType.ORDINAL` : the value is persisted as a Cassandra's `int` and
92 its value is its position in the enum.
93- `EnumType.STRING` : the value is persisted as a Cassandra's `text` and
94 its value is its name in the enum.
95
96According to the chosen type, the matching database column must be the
97same type.
98
99```
100CREATE TABLE person (id uuid PRIMARY KEY, name text, gender int);
101```
102
103```java
104@Enumerated(EnumType.ORDINAL)
105private Gender gender;
106```
107
108[enum]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Enumerated.html
109
110#### Computed fields
111
112[@Computed][computed] can be used on fields that are the result of a
113computation on the Cassandra side. Typically a function call. Native
114functions in Cassandra like `writetime()` or [User Defined Functions] are
115supported.
116
117```java
118@Computed("ttl(name)")
119Integer ttl;
120```
121
122The CQL return type of function must match the type of the field,
123otherwise an exception will be thrown.
124
125Computed fields are ignored when saving an entity.
126
127`@Computed` does not support case-sensitivity. If the expression
128contains case-sensitive column or function names, you'll have to escape
129them:
130
131```java
132@Computed("\"myFunction\"(\"myColumn\")")
133int f;
134```
135
136Finally, computed fields are only supported with [basic read
137operations](../using/#basic-crud-operations) at this time.
138Support in [accessors](../using/#accessors) in planned for a future
139version (see
140[JAVA-832](https://datastax-oss.atlassian.net/browse/JAVA-832)).
141
142[User Defined Functions]:http://www.planetcassandra.org/blog/user-defined-functions-in-cassandra-3-0/
143[computed]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Computed.html
144
145#### Transient fields
146
147[@Transient][transient] can be used to prevent a field from being mapped.
148
149[transient]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Transient.html
150
151### Mapping User Types
152
153[User Defined Types] can also be mapped by using [@UDT][udt]:
154
155```
156CREATE TYPE address (street text, zip_code int);
157```
158
159```java
160@UDT(keyspace = "ks", name = "address")
161class Address {
162 private String street;
163 @Field(name = "zip_code")
164 private int zipCode;
165 // ... constructors / getters / setters
166}
167```
168
169`@UDT` takes the following options:
170
171- `keyspace`: the keyspace for the UDT.
172- `name`: the name of the UDT in the database.
173- `caseSensitiveKeyspace`: whether the keyspace name is case-sensitive.
174- `caseSensitiveType`: whether the UDT name is case-sensitive.
175
176The class must provide a default constructor, and all fields (except the
177ones annotated `@Transient`) must have corresponding setters and
178getters.
179
180As in table entities, fields are mapped to an UDT field of the same
181name by default. To declare a different name or use case-sensitivity,
182use the [@Field][field] annotation:
183
184```java
185@Field(name = "zip_code")
186private int zipCode;
187```
188
189When a table has a UDT column, the mapper will automatically map it to
190the corresponding class:
191
192```
193CREATE TABLE company (company_id uuid PRIMARY KEY, name text, address address);
194```
195
196```java
197public static class Company {
198 @PartitionKey
199 @Column(name = "company_id")
200 private UUID companyId;
201 private String name;
202 private Address address;
203}
204```
205
206(this also works with UDTs inside collections or other UDTs, with
207arbitrary level of nesting)
208
209[User Defined Types]:http://docs.datastax.com/en/developer/java-driver/2.1/java-driver/reference/userDefinedTypes.html
210[udt]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/UDT.html
211[field]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Field.html
212
213### Mapping collections
214
215Java collections will be automatically mapped into corresponding
216Cassandra types. As in Cassandra, collections can contain all native
217types and all [user types](#mapping-user-types) previously defined is
218the database.
219
220Collection and UDT fields should be annotated to indicate whether they are
221frozen. Currently this is only for informational purposes (the mapper
222won't check that the declarations match the rules in Cassandra).
223However it is a good idea to keep using these annotations and make sure
224they match the schema, in anticipation for the schema generation features
225that will be added in a future version.
226The default annotation is [@Frozen][frozen], [@FrozenKey][frozenkey] and
227[@FrozenValue][frozenvalue]` are also provided for convenience:
228
229```java
230// Will be mapped as a 'list<text>'
231private List<String> stringList;
232
233// Will be mapped as a 'frozen<list<text>>'
234@Frozen
235private List<String> frozenStringList;
236
237// Will be mapped as 'map<frozen<address>, frozen<list<text>>>'
238@FrozenKey
239@FrozenValue
240private Map<Address, List<String>> frozenKeyValueMap;
241
242// Will be mapped as 'map<text, frozen<list<address>>>'
243@FrozenValue
244private Map<String, List<Address>> frozenValueMap;
245```
246
247[frozen]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Frozen.html
248[frozenkey]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/FrozenKey.html
249[frozenvalue]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/FrozenValue.html