kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 047e599c35f7fd9820cbb5b7428bece1321df7d25886950b64126f45c922e929 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-07-13 15:53 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-07-13 15:53 +02:00 |
| Parents | b0ad3785b703, cbf36bfc5f6a |
4 files changed
added features/object_mapper/README.md +18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +# Object Mapper | |
| 2 | + | |
| 3 | +Version 2.1 of the driver introduces a simple object mapper, which | |
| 4 | +avoids most of the boilerplate when converting your domain classes to | |
| 5 | +and from query results. It handles basic CRUD operations in Cassandra tables | |
| 6 | +containing UDTs, collections and all native CQL types. | |
| 7 | + | |
| 8 | +The mapper is published as a separate Maven artifact: | |
| 9 | + | |
| 10 | +```xml | |
| 11 | +<dependency> | |
| 12 | + <groupId>com.datastax.cassandra</groupId> | |
| 13 | + <artifactId>cassandra-driver-mapping</artifactId> | |
| 14 | + <version>2.1.6</version> | |
| 15 | +</dependency> | |
| 16 | +``` | |
| 17 | + | |
| 18 | +This documentation is organized in subsections describing mapper features. |
added features/object_mapper/creating/README.md +246 −0
| @@ -0,0 +1,246 @@ | ||
| 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/2.1/com/datastax/driver/mapping/annotations/Table.html | |
| 47 | +[case-sensitive]:http://docs.datastax.com/en/cql/3.2/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 | + | |
| 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/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 | |
| 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/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 | |
| 88 | +does not support enumerations, the object mapper defines two ways to | |
| 89 | +persist 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 | + | |
| 96 | +According to the chosen type, the matching database column must be the | |
| 97 | +same type. | |
| 98 | + | |
| 99 | +``` | |
| 100 | +CREATE TABLE person (id uuid PRIMARY KEY, name text, gender int); | |
| 101 | +``` | |
| 102 | + | |
| 103 | +```java | |
| 104 | +@Enumerated(EnumType.ORDINAL) | |
| 105 | +private 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 | |
| 113 | +computation on the Cassandra side. Typically a function call. Native | |
| 114 | +functions in Cassandra like `writetime()` or [User Defined Functions] are | |
| 115 | +supported. | |
| 116 | + | |
| 117 | +```java | |
| 118 | +@Computed("ttl(name)") | |
| 119 | +Integer ttl; | |
| 120 | +``` | |
| 121 | + | |
| 122 | +The CQL return type of function must match the type of the field, | |
| 123 | +otherwise an exception will be thrown. | |
| 124 | + | |
| 125 | +Computed fields are ignored when saving an entity. | |
| 126 | + | |
| 127 | +`@Computed` does not support case-sensitivity. If the expression | |
| 128 | +contains case-sensitive column or function names, you'll have to escape | |
| 129 | +them: | |
| 130 | + | |
| 131 | +```java | |
| 132 | +@Computed("\"myFunction\"(\"myColumn\")") | |
| 133 | +int f; | |
| 134 | +``` | |
| 135 | + | |
| 136 | +Finally, computed fields are only supported with [basic read | |
| 137 | +operations](../using/#basic-crud-operations) at this time. | |
| 138 | +Support in [accessors](../using/#accessors) in planned for a future | |
| 139 | +version (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 | +``` | |
| 156 | +CREATE TYPE address (street text, zip_code int); | |
| 157 | +``` | |
| 158 | + | |
| 159 | +```java | |
| 160 | +@UDT(keyspace = "ks", name = "address") | |
| 161 | +class 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 | + | |
| 176 | +The class must provide a default constructor, and all fields (except the | |
| 177 | +ones annotated `@Transient`) must have corresponding setters and | |
| 178 | +getters. | |
| 179 | + | |
| 180 | +As in table entities, fields are mapped to an UDT field of the same | |
| 181 | +name by default. To declare a different name or use case-sensitivity, | |
| 182 | +use the [@Field][field] annotation: | |
| 183 | + | |
| 184 | +```java | |
| 185 | +@Field(name = "zip_code") | |
| 186 | +private int zipCode; | |
| 187 | +``` | |
| 188 | + | |
| 189 | +When a table has a UDT column, the mapper will automatically map it to | |
| 190 | +the corresponding class: | |
| 191 | + | |
| 192 | +``` | |
| 193 | +CREATE TABLE company (company_id uuid PRIMARY KEY, name text, address address); | |
| 194 | +``` | |
| 195 | + | |
| 196 | +```java | |
| 197 | +public 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 | |
| 207 | +arbitrary 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 | + | |
| 215 | +Java collections will be automatically mapped into corresponding | |
| 216 | +Cassandra types. As in Cassandra, collections can contain all native | |
| 217 | +types and all [user types](#mapping-user-types) previously defined is | |
| 218 | +the database. | |
| 219 | + | |
| 220 | +Collection fields must be annotated to indicate whether they are frozen | |
| 221 | +or not (currently this is only for informational purposes, but the | |
| 222 | +mapper will check that the declared state match the rules in Cassandra). | |
| 223 | +The default annotation is [@Frozen][frozen], [@FrozenKey][frozenkey] and | |
| 224 | +[@FrozenValue][frozenvalue]` are also provided for convenience: | |
| 225 | + | |
| 226 | +```java | |
| 227 | +// Will be mapped as a 'list<text>' | |
| 228 | +private List<String> stringList; | |
| 229 | + | |
| 230 | +// Will be mapped as a 'frozen<list<text>>' | |
| 231 | +@Frozen | |
| 232 | +private List<String> frozenStringList; | |
| 233 | + | |
| 234 | +// Will be mapped as 'map<frozen<address>, frozen<list<text>>>' | |
| 235 | +@FrozenKey | |
| 236 | +@FrozenValue | |
| 237 | +private Map<Address, List<String>> frozenKeyValueMap; | |
| 238 | + | |
| 239 | +// Will be mapped as 'map<text, frozen<list<address>>>' | |
| 240 | +@FrozenValue | |
| 241 | +private Map<String, List<Address>> frozenValueMap; | |
| 242 | +``` | |
| 243 | + | |
| 244 | +[frozen]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Frozen.html | |
| 245 | +[frozenkey]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/FrozenKey.html | |
| 246 | +[frozenvalue]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/FrozenValue.html |
added features/object_mapper/using/README.md +312 −0
| @@ -0,0 +1,312 @@ | ||
| 1 | +## Using the mapper | |
| 2 | + | |
| 3 | +First, create a [MappingManager]. It wraps an existing [Session] | |
| 4 | +instance: | |
| 5 | + | |
| 6 | +```java | |
| 7 | +MappingManager manager = new MappingManager(session); | |
| 8 | +``` | |
| 9 | + | |
| 10 | +`MappingManager` is thread-safe and can be safely shared throughout your | |
| 11 | +application. You would typically create one instance at startup, right | |
| 12 | +after your `Session`. | |
| 13 | + | |
| 14 | +Note that `MappingManager` will initialize the `Session` if not | |
| 15 | +previously done (this was not the case in previous driver versions; if | |
| 16 | +this is a problem for you, see `MappingManager(session, protocolVersion)`). | |
| 17 | + | |
| 18 | +### Entity mappers | |
| 19 | + | |
| 20 | +Each entity class (annotated with `@Table`) is managed by a dedicated | |
| 21 | +[Mapper] object. You obtain this object from the `MappingManager`: | |
| 22 | + | |
| 23 | +```java | |
| 24 | +Mapper<User> mapper = manager.mapper(User.class); | |
| 25 | +``` | |
| 26 | + | |
| 27 | +`Mapper` objects are thread-safe. The manager caches them internally, so | |
| 28 | +calling `manager#mapper` more than once for the same class will return | |
| 29 | +the previously generated mapper. | |
| 30 | + | |
| 31 | +[Mapper]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/Mapper.html | |
| 32 | +[MappingManager]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/MappingManager.html | |
| 33 | +[Session]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html | |
| 34 | + | |
| 35 | +#### Basic CRUD operations | |
| 36 | + | |
| 37 | +To save an object, use `Mapper#save`: | |
| 38 | + | |
| 39 | +```java | |
| 40 | +UUID userId = ...; | |
| 41 | +User u = new User(userId, "John Doe", new Address("street", 01000)); | |
| 42 | +mapper.save(u); | |
| 43 | +``` | |
| 44 | + | |
| 45 | +-------------- | |
| 46 | + | |
| 47 | +To retrieve an object, use `Mapper#get`: | |
| 48 | + | |
| 49 | +```java | |
| 50 | +UUID userId = ...; | |
| 51 | +User u = mapper.get(userId); | |
| 52 | +``` | |
| 53 | + | |
| 54 | +`get`'s arguments must match the partition key components (number of | |
| 55 | +arguments and their types). | |
| 56 | + | |
| 57 | +-------------- | |
| 58 | + | |
| 59 | +To delete a row in a table, use `Mapper#delete`. This method support | |
| 60 | +deleting a row given either its primary keys, or the object to delete: | |
| 61 | + | |
| 62 | +```java | |
| 63 | +UUID userId = ...; | |
| 64 | +mapper.delete(userId); | |
| 65 | +mapper.delete(u); | |
| 66 | +``` | |
| 67 | + | |
| 68 | +-------------- | |
| 69 | + | |
| 70 | +All these CRUD operations are synchronous, but the `Mapper` provides | |
| 71 | +their asynchronous equivalents: | |
| 72 | + | |
| 73 | +```java | |
| 74 | +ListenableFuture<Void> saveFuture = mapper.saveAsync(u); | |
| 75 | +ListenableFuture<User> userFuture = mapper.getAsync(userId); | |
| 76 | +ListenableFuture<Void> deleteFuture = mapper.deleteAsync(userId); | |
| 77 | +``` | |
| 78 | + | |
| 79 | +#### Mapper options | |
| 80 | + | |
| 81 | +The basic CRUD operations accept additional options to customize the | |
| 82 | +underlying query: | |
| 83 | + | |
| 84 | +- `ttl`: add a time-to-live value for the operation. | |
| 85 | +- `timestamp`: add a timestamp value for the operation. | |
| 86 | +- `consistencyLevel`: specify a consistency level. | |
| 87 | +- `tracing`: set tracing flag for the query. | |
| 88 | +- `saveNullFields`: if set to true, fields with value `null` in an | |
| 89 | + instance that is to be persisted, will be explicitly written as `null` | |
| 90 | + in the query. If set to false, fields with null value won't be included | |
| 91 | + in the write query (thus avoiding tombstones). | |
| 92 | + | |
| 93 | +To use options, add them to the mapper call after regular parameters: | |
| 94 | + | |
| 95 | +```java | |
| 96 | +import static com.datastax.driver.mapping.Mapper.Option.*; | |
| 97 | + | |
| 98 | +mapper.save(new User(userId, "helloworld"), | |
| 99 | + timestamp(123456L), tracing(true), ttl(42)); | |
| 100 | +``` | |
| 101 | + | |
| 102 | +Some options don't apply to all operations: | |
| 103 | + | |
| 104 | +<table border="1" style="text-align:center; width:100%;margin-bottom:1em;"> | |
| 105 | + <tr> <td><b>Option</b></td> <td><b>save/saveQuery</b></td> <td><b>get/getQuery</b></td> <td><b>delete/deleteQuery</b></td></tr> | |
| 106 | + <tr> <td>Ttl</td> <td>yes</td> <td>no</td> <td>no</td> </tr> | |
| 107 | + <tr> <td>Timestamp</td> <td>yes</td> <td>no</td> <td>yes</td> </tr> | |
| 108 | + <tr> <td>ConsistencyLevel</td> <td>yes</td> <td>yes</td> <td>yes</td> </tr> | |
| 109 | + <tr> <td>Tracing</td> <td>yes</td> <td>yes</td> <td>yes</td> </tr> | |
| 110 | + <tr> <td>SaveNullFields</td> <td>yes</td> <td>no</td> <td>no</td> </tr> | |
| 111 | +</table> | |
| 112 | + | |
| 113 | +Note that `Option.consistencyLevel` is redundant with the consistency | |
| 114 | +level defined by [@Table](../creating/#creating-a-table-entity). | |
| 115 | +If both are defined, the option will take precedence over the | |
| 116 | +annotation. | |
| 117 | + | |
| 118 | +Default options can be defined for each type of operation: | |
| 119 | + | |
| 120 | +```java | |
| 121 | +mapper.setDefaultGetOption(tracing(true), consistencyLevel(QUORUM)); | |
| 122 | +mapper.setDefaultSaveOption(saveNullFields(false)); | |
| 123 | +mapper.setDefaultDeleteOption(consistencyLevel(ONE)); | |
| 124 | + | |
| 125 | +// Given the defaults above, this will use tracing(true), consistencyLevel(ONE) | |
| 126 | +mapper.get(uuid, consistencyLevel(ONE)); | |
| 127 | +``` | |
| 128 | + | |
| 129 | +To reset default options, use the following methods: | |
| 130 | + | |
| 131 | +```java | |
| 132 | +mapper.resetDefaultGetOption(); | |
| 133 | +mapper.resetDefaultSaveOption(); | |
| 134 | +mapper.resetDefaultDeleteOption(); | |
| 135 | +``` | |
| 136 | + | |
| 137 | +#### Access to underlying `Statement`s | |
| 138 | + | |
| 139 | +Instead of performing an operation directly, it's possible to ask the | |
| 140 | +`Mapper` to just return the corresponding `Statement` object. This gives | |
| 141 | +the client a chance to customize the statement before executing it. | |
| 142 | + | |
| 143 | +- `Mapper.saveQuery(entity)`: returns a statement generated by the | |
| 144 | + mapper to save `entity` into the database. | |
| 145 | +- `Mapper.getQuery(userId)`: returns a statement to select a row in the | |
| 146 | + database, selected on the given `userId`, and matching the mapped | |
| 147 | + object structure. | |
| 148 | +- `Mapper.deleteQuery(userID)`: returns a statement delete a row in the | |
| 149 | + database given the `userId` provided. This method can also accept a | |
| 150 | + mapped object instance. | |
| 151 | + | |
| 152 | +#### Manual mapping | |
| 153 | + | |
| 154 | +`Mapper#map` provides a way to converts the results of a regular query: | |
| 155 | + | |
| 156 | +```java | |
| 157 | +ResultSet results = session.execute("SELECT * FROM user"); | |
| 158 | +Result<User> users = mapper.map(results); | |
| 159 | +for (User u : users) { | |
| 160 | + System.out.println("User : " + u.getUserId()); | |
| 161 | +} | |
| 162 | +``` | |
| 163 | + | |
| 164 | +This method will ignore: | |
| 165 | + | |
| 166 | +* extra columns in the `ResultSet` that are not mapped for this entity. | |
| 167 | +* mapped fields that are not present in the `ResultSet` (setters won't | |
| 168 | + be called so the value will be the one after invocation of the class's | |
| 169 | + default constructor). | |
| 170 | + | |
| 171 | +[Result] is similar to `ResultSet` but for a given mapped class. | |
| 172 | +It provides methods `one()`, `all()`, `iterator()`, `getExecutionInfo()` | |
| 173 | +and `isExhausted()`. Note that iterating the `Result` will consume the | |
| 174 | +`ResultSet`, and vice-versa. | |
| 175 | + | |
| 176 | +[Result]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/Result.html | |
| 177 | + | |
| 178 | +### UDT Mappers | |
| 179 | + | |
| 180 | +A UDT will be automatically mapped when it is part of a table-level | |
| 181 | +class (as shown [here](../creating/#mapping-user-types)). | |
| 182 | + | |
| 183 | +However, there might be cases where it is useful to convert a mapped | |
| 184 | +instance to the corresponding [UDTValue][UDTValue] (and back). The | |
| 185 | +mapper provides an [UDTMapper] for that purpose: | |
| 186 | + | |
| 187 | +```java | |
| 188 | +UDTMapper<Address> addressMapper = manager.getUDTMapper(Address.class); | |
| 189 | +Address address = addressMapper.fromUDT(udtValue); | |
| 190 | +UDTValue value = addressMapper.toUDT(address); | |
| 191 | +``` | |
| 192 | + | |
| 193 | +[UDTValue]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/UDTValue.html | |
| 194 | +[UDTMapper]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/UDTMapper.html | |
| 195 | + | |
| 196 | +### Accessors | |
| 197 | + | |
| 198 | +`Accessor`s provide a way to map custom queries not supported by the | |
| 199 | +default entity mappers. | |
| 200 | + | |
| 201 | +To create an accessor, define a Java interface and annotate each method | |
| 202 | +to provide the corresponding CQL query: | |
| 203 | + | |
| 204 | +```java | |
| 205 | +@Accessor | |
| 206 | +public interface UserAccessor { | |
| 207 | + @Query("SELECT * FROM user") | |
| 208 | + Result<User> getAll(); | |
| 209 | +} | |
| 210 | +``` | |
| 211 | + | |
| 212 | +The `MappingManager` can then process this class and generate an | |
| 213 | +implementation: | |
| 214 | + | |
| 215 | +```java | |
| 216 | +UserAccessor userAccessor = manager.createAccessor(UserAccessor.class); | |
| 217 | +User user = userAccessor.getOne(uuid); | |
| 218 | +``` | |
| 219 | + | |
| 220 | +Like mappers, accessors are cached at the manager level and thus, are | |
| 221 | +thread-safe/sharable. | |
| 222 | + | |
| 223 | +#### Parameters | |
| 224 | + | |
| 225 | +A query can have bind markers, that will be set with the method's | |
| 226 | +arguments. | |
| 227 | + | |
| 228 | +With unnamed markers, the order of the arguments must match the order of | |
| 229 | +the markers: | |
| 230 | + | |
| 231 | +```java | |
| 232 | +@Query("insert into user (id, name) values (?, ?)") | |
| 233 | +ResultSet insert(UUID userId, String name); | |
| 234 | +``` | |
| 235 | + | |
| 236 | +With named markers, use [@Param][param] to indicate which parameter | |
| 237 | +corresponds to which marker: | |
| 238 | + | |
| 239 | +```java | |
| 240 | +@Query("insert into user (userId, name) values (:u, :n)") | |
| 241 | +ResultSet insert(@Param("u") UUID userId, @Param("n") String name); | |
| 242 | +``` | |
| 243 | + | |
| 244 | +[param]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/Param.html | |
| 245 | + | |
| 246 | +If a method argument is a Java enumeration, it must be annotated with | |
| 247 | +`@Enumerated` to indicate how to convert it to a CQL type (the rules are | |
| 248 | +the same as in [mapping definition](http://localhost:9090/features/object_mapper/creating/#enumerations)): | |
| 249 | + | |
| 250 | +```java | |
| 251 | +@Query("insert into user (key, gender) values (?,?)") | |
| 252 | +ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value); | |
| 253 | +``` | |
| 254 | + | |
| 255 | +#### Return type | |
| 256 | + | |
| 257 | +The declared return type of each method affects how the query will get | |
| 258 | +executed: | |
| 259 | + | |
| 260 | +<table border="1" style="text-align:center; width:100%;margin-bottom:1em;"> | |
| 261 | + <tr> <td><b>Return type</b></td> <td><b>Effect</b></td> </tr> | |
| 262 | + <tr> | |
| 263 | + <td><code>void</code></td> | |
| 264 | + <td>Synchronous execution, discards the results of the query.</td> | |
| 265 | + </tr> | |
| 266 | + <tr> | |
| 267 | + <td><code>ResultSet</code></td> | |
| 268 | + <td>Synchronous execution, returns unmapped results.</td> | |
| 269 | + </tr> | |
| 270 | + <tr> | |
| 271 | + <td><code>T</code></td> | |
| 272 | + <td><code>T</code> must be a mapped class.<br/>Synchronous execution, returns the first row (or <code>null</code> if there are no results).</td> | |
| 273 | + </tr> | |
| 274 | + <tr> | |
| 275 | + <td><code>Result<T></code></td> | |
| 276 | + <td><code>T</code> must be a mapped class.<br/>Synchronous execution, returns a list of mapped objects.</td> | |
| 277 | + </tr> | |
| 278 | + <tr> | |
| 279 | + <td><code>ResultSetFuture</code></td> | |
| 280 | + <td>Asynchronous execution, returns unmapped results.</td> | |
| 281 | + </tr> | |
| 282 | + <tr> | |
| 283 | + <td><code>ListenableFuture<T></code></td> | |
| 284 | + <td><code>T</code> must be a mapped class.<br/>Asynchronous execution, returns the first row (or <code>null</code> if there are no results).</td> | |
| 285 | + </tr> | |
| 286 | + <tr> | |
| 287 | + <td><code>ListenableFuture<Result<T>></code></td> | |
| 288 | + <td><code>T</code> must be a mapped class.<br/>Asynchronous execution, returns a list of mapped objects.</td> | |
| 289 | + </tr> | |
| 290 | +</table> | |
| 291 | + | |
| 292 | +Example: | |
| 293 | + | |
| 294 | +```java | |
| 295 | +@Query("SELECT * FROM user") | |
| 296 | +public ListenableFuture<Result<User>> getAllAsync(); | |
| 297 | +``` | |
| 298 | + | |
| 299 | + | |
| 300 | +#### Customizing the statement | |
| 301 | + | |
| 302 | +It is possible to customize query parameters to include in a `Accessor` | |
| 303 | +query with the annotation [@QueryParameters]. Then, options like | |
| 304 | +*consistency level*, *fetchsize* or *tracing* are settable: | |
| 305 | + | |
| 306 | +```java | |
| 307 | +@Query("SELECT * FROM ks.users") | |
| 308 | +@QueryParameters(consistency="QUORUM") | |
| 309 | +public ListenableFuture<Result<User>> getAllAsync(); | |
| 310 | +``` | |
| 311 | + | |
| 312 | +[@QueryParameters]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/annotations/QueryParameters.html |
modified features/shaded_jar/README.md +2 −2
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | |
| 11 | 11 | ```xml |
| 12 | 12 | <dependency> |
| 13 | - <groupId>${project.groupId}</groupId> | |
| 13 | + <groupId>com.datastax.cassandra</groupId> | |
| 14 | 14 | <artifactId>cassandra-driver-core</artifactId> |
| 15 | 15 | <version>2.1.6</version> |
| 16 | 16 | <classifier>shaded</classifier> |
| @@ -52,4 +52,4 @@ | ||
| 52 | 52 | </exclusion> |
| 53 | 53 | </exclusions> |
| 54 | 54 | </dependency> |
| 55 | -``` | |
| No newline at the end of the file | ||
| 55 | +``` |