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