Elffuss BraidHub

kikocis/java-driver public

3.1.x · 3.7 KiBRenderedRawPermalink
1# Using custom codecs
2
3The mapper can take advantage of [custom codecs](../../custom_codecs/)
4to apply custom conversions to mapped columns and fields.
5
6## Declaring codecs
7
8Let's assume you have a table containing a timestamp column:
9
10```
11create table user(id int primary key, birth timestamp);
12```
13
14You've created a custom class, and the codec to convert it:
15
16```java
17public class MyCustomDate { ... }
18
19public class MyCustomDateCodec extends TypeCodec<MyCustomDate> {
20 public MyCustomDate() {
21 super(DataType.timestamp(), MyCustomDate.class);
22 }
23 ...
24}
25```
26
27### Pre-registered codecs
28
29If you register your codec with the mapper's underlying `Cluster`, it
30will be automatically available to the mapper:
31
32```java
33Cluster cluster = Cluster.builder()
34 .addContactPoint("127.0.0.1")
35 .withCodecRegistry(
36 new CodecRegistry().register(new MyCustomDateCodec())
37 ).build();
38
39MappingManager mappingManager = new MappingManager(cluster.connect());
40```
41
42You can normally create your mapped classes using your custom type,
43without any additional configuration:
44
45```java
46@Table(name = "user")
47public class User {
48 @PartitionKey
49 private int id;
50 private MyCustomDate birth;
51
52 ... // getters and setters
53}
54```
55
56This also works in accessors:
57
58```java
59@Accessor
60interface UserAccessor {
61 @Query("update user set birth = :b where id = :i")
62 void updateBirth(@Param("i") int id,
63 @Param("b") MyCustomDate birth);
64}
65```
66
67### One-time declaration
68
69Sometimes you might want to use your codec only for one particular
70column/field. In that case you won't register it when initializing the
71`Cluster`:
72
73```java
74Cluster cluster = Cluster.builder()
75 .addContactPoint("127.0.0.1")
76 .build();
77
78MappingManager mappingManager = new MappingManager(cluster.connect());
79```
80
81Instead, reference the codec's class in the [@Column][column]
82annotation:
83
84```java
85@Table(name = "user")
86public class User {
87 @PartitionKey
88 private int id;
89 @Column(codec = MyCustomDateCodec.class)
90 private MyCustomDate birth;
91
92 ... // getters and setters
93}
94```
95
96The class must have a no-arg constructor. The mapper will create an
97instance (one per column) and cache it for future use.
98
99This also works with [@Field][field] and [@Param][param] annotations.
100
101[column]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Column.html
102[field]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Field.html
103[param]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/Param.html
104
105
106## Implicit UDT codecs
107
108The mapper uses custom codecs internally to handle UDT conversions: when
109you register an [entity](../using/#entity-mappers), the mapper inspects
110the type of all fields to find classes annotated with
111[@UDT](../creating/#mapping-user-types) (this works recursively with
112nested UDTs and collections). For each class, the mapper creates a codec
113and registers it with the underlying `Cluster`.
114
115```java
116@UDT(name = "address")
117public class Address { ... }
118
119@Entity(name = "user")
120public class User {
121 ...
122 private Address address;
123 ...
124}
125
126Mapper<User> userMapper = mappingManager.mapper(User.class);
127
128// Codec is now registered for Address <-> CQL address
129```
130
131A nice side-effect is that you can now use the `@UDT`-annotated class
132with any driver method, not just mapper methods:
133
134```java
135Row row = session.execute("select address from user where id = 1").one();
136Address address = row.get("address", Address.class);
137```
138
139If you don't use entity mappers but still want the convenience of the
140UDT codec for core driver methods, the mapper provides a way to create
141it independently:
142
143```java
144mappingManager.udtCodec(Address.class);
145
146// Codec is now registered
147```