kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 01c1288c6b3c2085e6274fd1ba2695f9ee607a61db12f5e0501dbeb146cdb832 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-02 17:01 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-17 14:52 +01:00 |
| Parents | 70f21c53e06b |
18 files changed
modified changelog/README.md +1 −0
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | - [new feature] JAVA-742: Codec Support for JSON. |
| 19 | 19 | - [new feature] JAVA-606: Codec support for Java 8. |
| 20 | 20 | - [new feature] JAVA-565: Codec support for Java arrays. |
| 21 | +- [new feature] JAVA-605: Codec support for Java enums. | |
| 21 | 22 | |
| 22 | 23 | Merged from 2.1 branch: |
| 23 | 24 |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/enums/EnumCodecsTest.java +55 −22
| @@ -31,12 +31,9 @@ | ||
| 31 | 31 | |
| 32 | 32 | import com.datastax.driver.core.*; |
| 33 | 33 | import com.datastax.driver.core.utils.CassandraVersion; |
| 34 | -import com.datastax.driver.mapping.EnumType; | |
| 35 | 34 | import com.datastax.driver.mapping.Mapper; |
| 36 | 35 | import com.datastax.driver.mapping.MappingManager; |
| 37 | -import com.datastax.driver.mapping.annotations.Enumerated; | |
| 38 | -import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 39 | -import com.datastax.driver.mapping.annotations.Table; | |
| 36 | +import com.datastax.driver.mapping.annotations.*; | |
| 40 | 37 | |
| 41 | 38 | import static com.datastax.driver.core.DataType.cint; |
| 42 | 39 | import static com.datastax.driver.core.DataType.text; |
| @@ -72,15 +69,15 @@ | ||
| 72 | 69 | + "foo int," |
| 73 | 70 | + "bar text)", |
| 74 | 71 | "CREATE TABLE IF NOT EXISTS t1 (" |
| 75 | - + "pk int PRIMARY KEY, " | |
| 72 | + + "pk int, " | |
| 76 | 73 | + "foo int, " |
| 77 | 74 | + "foos list<int>, " |
| 78 | 75 | + "bar text, " |
| 79 | 76 | + "bars set<text>, " |
| 80 | 77 | + "foobars map<int,text>, " |
| 81 | 78 | + "tup frozen<tuple<int,varchar>>, " |
| 82 | - + "udt frozen<udt1>" | |
| 83 | - + ")" | |
| 79 | + + "udt frozen<udt1>," | |
| 80 | + + "primary key (pk, foo))" | |
| 84 | 81 | ); |
| 85 | 82 | } |
| 86 | 83 | |
| @@ -143,18 +140,54 @@ | ||
| 143 | 140 | } |
| 144 | 141 | |
| 145 | 142 | @Test(groups = "short") |
| 146 | - public void should_use_mapper_to_store_and_retrieve_values_with_enum_codecs() { | |
| 143 | + public void should_use_mapper_to_store_and_retrieve_nulls_with_enum_codecs() { | |
| 147 | 144 | // given |
| 148 | 145 | MappingManager manager = new MappingManager(session); |
| 149 | 146 | Mapper<Mapped> mapper = manager.mapper(Mapped.class); |
| 147 | + Mapped pojo = new Mapped(); | |
| 148 | + pojo.pk = 42; | |
| 149 | + pojo.foo = FOO_1; | |
| 150 | 150 | // when |
| 151 | + mapper.save(pojo); | |
| 152 | + Mapped actual = mapper.get(42, FOO_1); | |
| 153 | + // then | |
| 154 | + assertThat(actual).isEqualToComparingFieldByField(pojo); | |
| 155 | + } | |
| 156 | + | |
| 157 | + @Test(groups = "short") | |
| 158 | + public void should_use_mapper_to_store_and_retrieve_values_with_enum_codecs() { | |
| 159 | + // given | |
| 160 | + MappingManager manager = new MappingManager(session); | |
| 161 | + Mapper<Mapped> mapper = manager.mapper(Mapped.class); | |
| 151 | 162 | Mapped pojo = new Mapped(); |
| 163 | + pojo.pk = 42; | |
| 164 | + pojo.foo = FOO_1; | |
| 165 | + pojo.bar = BAR_1; | |
| 166 | + pojo.foos = newArrayList(FOO_2, FOO_1); | |
| 167 | + pojo.bars = newHashSet(BAR_1, BAR_2); | |
| 168 | + pojo.foobars = ImmutableMap.of(FOO_1, BAR_2); | |
| 169 | + // when | |
| 152 | 170 | mapper.save(pojo); |
| 153 | - Mapped actual = mapper.get(42); | |
| 171 | + Mapped actual = mapper.get(42, FOO_1); | |
| 154 | 172 | // then |
| 155 | 173 | assertThat(actual).isEqualToComparingFieldByField(pojo); |
| 156 | 174 | } |
| 157 | 175 | |
| 176 | + @Test(groups = "short") | |
| 177 | + public void should_use_accessor_to_store_and_retrieve_values_with_enum_codecs() { | |
| 178 | + // given | |
| 179 | + MappedAccessor accessor = new MappingManager(session).createAccessor(MappedAccessor.class); | |
| 180 | + Mapped expected = new Mapped(); | |
| 181 | + expected.pk = 42; | |
| 182 | + expected.foo = FOO_1; | |
| 183 | + expected.bar = BAR_2; | |
| 184 | + // when | |
| 185 | + accessor.insert(42, FOO_1, BAR_2); | |
| 186 | + Mapped result = accessor.getByFoo(42, FOO_1); | |
| 187 | + // then | |
| 188 | + assertThat(result).isEqualToComparingFieldByField(expected); | |
| 189 | + } | |
| 190 | + | |
| 158 | 191 | private void assertRow(Row row) { |
| 159 | 192 | |
| 160 | 193 | assertThat(row.getInt(0)).isEqualTo(pk); |
| @@ -215,10 +248,7 @@ | ||
| 215 | 248 | @PartitionKey |
| 216 | 249 | private int pk; |
| 217 | 250 | |
| 218 | - // note that we need to specify @Enumerated here | |
| 219 | - // because the mapper already has a partial support for enums | |
| 220 | - // that kicks in for fields of type Enum | |
| 221 | - @Enumerated(EnumType.ORDINAL) | |
| 251 | + @ClusteringColumn | |
| 222 | 252 | private Foo foo; |
| 223 | 253 | |
| 224 | 254 | private List<Foo> foos; |
| @@ -229,15 +259,6 @@ | ||
| 229 | 259 | |
| 230 | 260 | private Map<Foo, Bar> foobars; |
| 231 | 261 | |
| 232 | - public Mapped(){ | |
| 233 | - pk = 42; | |
| 234 | - foo = FOO_1; | |
| 235 | - bar = BAR_1; | |
| 236 | - foos = newArrayList(FOO_2, FOO_1); | |
| 237 | - bars = newHashSet(BAR_1, BAR_2); | |
| 238 | - foobars = ImmutableMap.of(FOO_1, BAR_2); | |
| 239 | - } | |
| 240 | - | |
| 241 | 262 | public int getPk() { |
| 242 | 263 | return pk; |
| 243 | 264 | } |
| @@ -287,6 +308,18 @@ | ||
| 287 | 308 | } |
| 288 | 309 | } |
| 289 | 310 | |
| 311 | + @Accessor | |
| 312 | + public interface MappedAccessor { | |
| 313 | + | |
| 314 | + @Query("select * from t1 where pk=? and foo=?") | |
| 315 | + Mapped getByFoo(int pk, Foo foo); | |
| 316 | + | |
| 317 | + @Query("insert into t1 (pk, foo, bar) values (?,?,?)") | |
| 318 | + ResultSet insert(int pk, Foo foo, Bar bar); | |
| 319 | + | |
| 320 | + } | |
| 321 | + | |
| 322 | + | |
| 290 | 323 | enum Foo { |
| 291 | 324 | FOO_1, FOO_2 |
| 292 | 325 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +2 −22
| @@ -30,7 +30,6 @@ | ||
| 30 | 30 | import com.datastax.driver.core.ConsistencyLevel; |
| 31 | 31 | import com.datastax.driver.core.TypeCodec; |
| 32 | 32 | import com.datastax.driver.core.UserType; |
| 33 | -import com.datastax.driver.mapping.MethodMapper.EnumParamMapper; | |
| 34 | 33 | import com.datastax.driver.mapping.MethodMapper.ParamMapper; |
| 35 | 34 | import com.datastax.driver.mapping.annotations.*; |
| 36 | 35 | |
| @@ -80,7 +79,7 @@ | ||
| 80 | 79 | throw new UnsupportedOperationException("Computed fields are not supported with native protocol v1"); |
| 81 | 80 | |
| 82 | 81 | AnnotationChecks.validateAnnotations(field, "entity", |
| 83 | - Column.class, ClusteringColumn.class, Enumerated.class, Frozen.class, FrozenKey.class, | |
| 82 | + Column.class, ClusteringColumn.class, Frozen.class, FrozenKey.class, | |
| 84 | 83 | FrozenValue.class, PartitionKey.class, Transient.class, Computed.class); |
| 85 | 84 | |
| 86 | 85 | if (field.getAnnotation(Transient.class) != null) |
| @@ -139,7 +138,7 @@ | ||
| 139 | 138 | |
| 140 | 139 | AnnotationChecks.validateAnnotations(field, "UDT", |
| 141 | 140 | com.datastax.driver.mapping.annotations.Field.class, Frozen.class, FrozenKey.class, |
| 142 | - FrozenValue.class, Enumerated.class, Transient.class); | |
| 141 | + FrozenValue.class, Transient.class); | |
| 143 | 142 | |
| 144 | 143 | if (field.getAnnotation(Transient.class) != null) |
| 145 | 144 | continue; |
| @@ -208,15 +207,6 @@ | ||
| 208 | 207 | return ColumnMapper.Kind.REGULAR; |
| 209 | 208 | } |
| 210 | 209 | |
| 211 | - public static EnumType enumType(Field field) { | |
| 212 | - Class<?> type = field.getType(); | |
| 213 | - if (!type.isEnum()) | |
| 214 | - return null; | |
| 215 | - | |
| 216 | - Enumerated enumerated = field.getAnnotation(Enumerated.class); | |
| 217 | - return (enumerated == null) ? EnumType.STRING : enumerated.value(); | |
| 218 | - } | |
| 219 | - | |
| 220 | 210 | public static String columnName(Field field) { |
| 221 | 211 | Column column = field.getAnnotation(Column.class); |
| 222 | 212 | Computed computedField = field.getAnnotation(Computed.class); |
| @@ -337,16 +327,6 @@ | ||
| 337 | 327 | private static ParamMapper newParamMapper(String className, String methodName, int idx, String paramName, Class<? extends TypeCodec<?>> codecClass, Type paramType, Annotation[] paramAnnotations, MappingManager mappingManager) { |
| 338 | 328 | if (paramType instanceof Class) { |
| 339 | 329 | Class<?> paramClass = (Class<?>) paramType; |
| 340 | - if (paramClass.isEnum()) { | |
| 341 | - EnumType enumType = EnumType.STRING; | |
| 342 | - for (Annotation annotation : paramAnnotations) { | |
| 343 | - if (annotation instanceof Enumerated) { | |
| 344 | - enumType = ((Enumerated) annotation).value(); | |
| 345 | - } | |
| 346 | - } | |
| 347 | - return new EnumParamMapper(paramName, idx, enumType); | |
| 348 | - } | |
| 349 | - | |
| 350 | 330 | if (TypeMappings.isMappedUDT(paramClass)) |
| 351 | 331 | mappingManager.getUDTCodec(paramClass); |
| 352 | 332 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ColumnMapper.java +2 −3
| @@ -69,10 +69,9 @@ | ||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | - * The Java type that will get passed to the codec. | |
| 73 | - * This might be different from the field type if the column mapper does a transformation. | |
| 72 | + * The Java type of this column. | |
| 74 | 73 | */ |
| 75 | - public TypeToken<Object> getPivotType() { | |
| 74 | + public TypeToken<Object> getJavaType() { | |
| 76 | 75 | return fieldType; |
| 77 | 76 | } |
| 78 | 77 | } |
deleted driver-mapping/src/main/java/com/datastax/driver/mapping/EnumType.java +0 −41
| @@ -1,41 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.mapping; | |
| 17 | - | |
| 18 | -import com.google.common.reflect.TypeToken; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * The types of way to persist a JAVA Enum. | |
| 22 | - */ | |
| 23 | -public enum EnumType { | |
| 24 | - /** | |
| 25 | - * Persists enumeration values using their ordinal in the Enum declaration. | |
| 26 | - * <p> | |
| 27 | - * Note that this relies on the order of the values in source code, and will change | |
| 28 | - * if values are added/removed before existing ones. | |
| 29 | - */ | |
| 30 | - ORDINAL(Integer.class), | |
| 31 | - /** Persists enumeration values using the string they have been declared with. */ | |
| 32 | - STRING(String.class); | |
| 33 | - | |
| 34 | - @SuppressWarnings("unchecked") | |
| 35 | - EnumType(Class<?> klass) { | |
| 36 | - this.pivotType = (TypeToken<Object>)TypeToken.of(klass); | |
| 37 | - } | |
| 38 | - | |
| 39 | - /** The type enum constants will be turned into before being saved */ | |
| 40 | - final TypeToken<Object> pivotType; | |
| 41 | -} |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MappedUDTCodec.java +2 −2
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | Object value = cm.getValue(sourceObject); |
| 53 | 53 | TypeCodec<Object> codec = cm.getCustomCodec(); |
| 54 | 54 | if (codec == null) |
| 55 | - codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getPivotType()); | |
| 55 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getJavaType()); | |
| 56 | 56 | ByteBuffer serializedField = codec.serialize(value, protocolVersion); |
| 57 | 57 | size += 4 + (serializedField == null ? 0 : serializedField.remaining()); |
| 58 | 58 | serializedFields.add(serializedField); |
| @@ -83,7 +83,7 @@ | ||
| 83 | 83 | ByteBuffer serialized = (size < 0) ? null : CodecUtils.readBytes(input, size); |
| 84 | 84 | TypeCodec<Object> codec = cm.getCustomCodec(); |
| 85 | 85 | if (codec == null) |
| 86 | - codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getPivotType()); | |
| 86 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getJavaType()); | |
| 87 | 87 | cm.setValue(targetObject, codec.deserialize(serialized, protocolVersion)); |
| 88 | 88 | } |
| 89 | 89 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +1 −1
| @@ -226,7 +226,7 @@ | ||
| 226 | 226 | if (customCodec != null) |
| 227 | 227 | bs.set(i, value, customCodec); |
| 228 | 228 | else |
| 229 | - bs.set(i, value, mapper.getPivotType()); | |
| 229 | + bs.set(i, value, mapper.getJavaType()); | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +2 −30
| @@ -143,7 +143,7 @@ | ||
| 143 | 143 | |
| 144 | 144 | ProtocolVersion protocolVersion = session.getCluster().getConfiguration().getProtocolOptions().getProtocolVersion(); |
| 145 | 145 | for (int i = 0; i < args.length; i++) { |
| 146 | - paramMappers[i].setValue(bs, args[i], protocolVersion); | |
| 146 | + paramMappers[i].setValue(bs, args[i]); | |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | if (consistency != null) |
| @@ -196,7 +196,7 @@ | ||
| 196 | 196 | } |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) { | |
| 199 | + void setValue(BoundStatement boundStatement, Object arg) { | |
| 200 | 200 | if (paramName == null) { |
| 201 | 201 | if (codec == null) |
| 202 | 202 | boundStatement.set(paramIdx, arg, paramType); |
| @@ -211,32 +211,4 @@ | ||
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - static class EnumParamMapper extends ParamMapper { | |
| 215 | - | |
| 216 | - private final EnumType enumType; | |
| 217 | - | |
| 218 | - public EnumParamMapper(String paramName, int paramIdx, EnumType enumType) { | |
| 219 | - super(paramName, paramIdx, enumType.pivotType, null); | |
| 220 | - this.enumType = enumType; | |
| 221 | - } | |
| 222 | - | |
| 223 | - @Override | |
| 224 | - void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) { | |
| 225 | - super.setValue(boundStatement, convert(arg), protocolVersion); | |
| 226 | - } | |
| 227 | - | |
| 228 | - @SuppressWarnings("rawtypes") | |
| 229 | - private Object convert(Object arg) { | |
| 230 | - if(arg == null) | |
| 231 | - return arg; | |
| 232 | - | |
| 233 | - switch (enumType) { | |
| 234 | - case STRING: | |
| 235 | - return arg.toString(); | |
| 236 | - case ORDINAL: | |
| 237 | - return ((Enum) arg).ordinal(); | |
| 238 | - } | |
| 239 | - throw new AssertionError(); | |
| 240 | - } | |
| 241 | - } | |
| 242 | 214 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +0 −4
| @@ -58,10 +58,6 @@ | ||
| 58 | 58 | this.endInclusive = endInclusive; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - public static QueryType slice(int startBoundSize, boolean startInclusive, int endBoundSize, boolean endInclusive, boolean reversed) { | |
| 62 | - return new QueryType(reversed ? Kind.REVERSED_SLICE : Kind.SLICE, startBoundSize, startInclusive, endBoundSize, endInclusive); | |
| 63 | - } | |
| 64 | - | |
| 65 | 61 | String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<ColumnMapper<?>> columns, Collection<Mapper.Option> options) { |
| 66 | 62 | QueryBuilder builder = new QueryBuilder(manager.getSession().getCluster()); |
| 67 | 63 | switch (kind) { |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionMapper.java +0 −60
| @@ -19,12 +19,8 @@ | ||
| 19 | 19 | import java.beans.PropertyDescriptor; |
| 20 | 20 | import java.lang.reflect.Field; |
| 21 | 21 | import java.lang.reflect.Method; |
| 22 | -import java.util.HashMap; | |
| 23 | -import java.util.Map; | |
| 24 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | 23 | |
| 26 | -import com.google.common.reflect.TypeToken; | |
| 27 | - | |
| 28 | 24 | import com.datastax.driver.core.ConsistencyLevel; |
| 29 | 25 | |
| 30 | 26 | /** |
| @@ -86,58 +82,6 @@ | ||
| 86 | 82 | } |
| 87 | 83 | } |
| 88 | 84 | |
| 89 | - private static class EnumMapper<T> extends LiteralMapper<T> { | |
| 90 | - | |
| 91 | - private final EnumType enumType; | |
| 92 | - private final Map<String, Object> fromString; | |
| 93 | - | |
| 94 | - private EnumMapper(Field field, int position, PropertyDescriptor pd, EnumType enumType, AtomicInteger columnCounter) { | |
| 95 | - super(field, position, pd, columnCounter); | |
| 96 | - this.enumType = enumType; | |
| 97 | - | |
| 98 | - if (enumType == EnumType.STRING) { | |
| 99 | - fromString = new HashMap<String, Object>(fieldType.getRawType().getEnumConstants().length); | |
| 100 | - for (Object constant : fieldType.getRawType().getEnumConstants()) | |
| 101 | - fromString.put(constant.toString().toLowerCase(), constant); | |
| 102 | - | |
| 103 | - } else { | |
| 104 | - fromString = null; | |
| 105 | - } | |
| 106 | - } | |
| 107 | - | |
| 108 | - @SuppressWarnings("rawtypes") | |
| 109 | - @Override | |
| 110 | - public Object getValue(T entity) { | |
| 111 | - Object value = super.getValue(entity); | |
| 112 | - switch (enumType) { | |
| 113 | - case STRING: | |
| 114 | - return (value == null) ? null : value.toString(); | |
| 115 | - case ORDINAL: | |
| 116 | - return (value == null) ? null : ((Enum)value).ordinal(); | |
| 117 | - } | |
| 118 | - throw new AssertionError(); | |
| 119 | - } | |
| 120 | - | |
| 121 | - @Override | |
| 122 | - public void setValue(Object entity, Object value) { | |
| 123 | - Object converted = null; | |
| 124 | - switch (enumType) { | |
| 125 | - case STRING: | |
| 126 | - converted = fromString.get(value.toString().toLowerCase()); | |
| 127 | - break; | |
| 128 | - case ORDINAL: | |
| 129 | - converted = fieldType.getRawType().getEnumConstants()[(Integer)value]; | |
| 130 | - break; | |
| 131 | - } | |
| 132 | - super.setValue(entity, converted); | |
| 133 | - } | |
| 134 | - | |
| 135 | - @Override | |
| 136 | - public TypeToken<Object> getPivotType() { | |
| 137 | - return enumType.pivotType; | |
| 138 | - } | |
| 139 | - } | |
| 140 | - | |
| 141 | 85 | private static class ReflectionFactory implements Factory { |
| 142 | 86 | |
| 143 | 87 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| @@ -150,10 +94,6 @@ | ||
| 150 | 94 | try { |
| 151 | 95 | PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass()); |
| 152 | 96 | |
| 153 | - if (field.getType().isEnum()) { | |
| 154 | - return new EnumMapper<T>(field, position, pd, AnnotationParser.enumType(field), columnCounter); | |
| 155 | - } | |
| 156 | - | |
| 157 | 97 | for (Class<?> udt : TypeMappings.findUDTs(field.getGenericType())) |
| 158 | 98 | mappingManager.getUDTCodec(udt); |
| 159 | 99 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java +1 −1
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | if (customCodec != null) |
| 53 | 53 | value = row.get(name, customCodec); |
| 54 | 54 | else |
| 55 | - value = row.get(name, cm.getPivotType()); | |
| 55 | + value = row.get(name, cm.getJavaType()); | |
| 56 | 56 | |
| 57 | 57 | if (shouldSetValue(value)) { |
| 58 | 58 | cm.setValue(entity, value); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/TypeMappings.java +0 −8
| @@ -24,8 +24,6 @@ | ||
| 24 | 24 | |
| 25 | 25 | import com.google.common.collect.Sets; |
| 26 | 26 | |
| 27 | -import com.datastax.driver.core.TupleValue; | |
| 28 | -import com.datastax.driver.core.UDTValue; | |
| 29 | 27 | import com.datastax.driver.mapping.annotations.UDT; |
| 30 | 28 | |
| 31 | 29 | /** |
| @@ -53,12 +51,6 @@ | ||
| 53 | 51 | return klass.isAnnotationPresent(UDT.class); |
| 54 | 52 | } |
| 55 | 53 | |
| 56 | - static boolean mapsToUserTypeOrTuple(Class<?> klass) { | |
| 57 | - return isMappedUDT(klass) || | |
| 58 | - klass.equals(UDTValue.class) || | |
| 59 | - klass.equals(TupleValue.class); | |
| 60 | - } | |
| 61 | - | |
| 62 | 54 | /** |
| 63 | 55 | * Traverses the type of a Java field or parameter to find classes annotated with @UDT. |
| 64 | 56 | * This will recurse into nested collections, e.g. List<Set<TheMappedUDT>> |
deleted driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Enumerated.java +0 −41
| @@ -1,41 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.mapping.annotations; | |
| 17 | - | |
| 18 | -import java.lang.annotation.ElementType; | |
| 19 | -import java.lang.annotation.Retention; | |
| 20 | -import java.lang.annotation.RetentionPolicy; | |
| 21 | -import java.lang.annotation.Target; | |
| 22 | - | |
| 23 | -import com.datastax.driver.mapping.EnumType; | |
| 24 | - | |
| 25 | -/** | |
| 26 | - * Defines that the annotated field (that must be a Java Enum) must be | |
| 27 | - * persisted as an enumerated type. | |
| 28 | - * <p> | |
| 29 | - * The optional {@link EnumType} value defined how the enumeration must be | |
| 30 | - * persisted. | |
| 31 | - */ | |
| 32 | -@Target(value = {ElementType.FIELD, ElementType.PARAMETER}) | |
| 33 | -@Retention(RetentionPolicy.RUNTIME) | |
| 34 | -public @interface Enumerated { | |
| 35 | - /** | |
| 36 | - * How the enumeration must be persisted. | |
| 37 | - * | |
| 38 | - * @return the {@link EnumType} to use for mapping this enumeration. | |
| 39 | - */ | |
| 40 | - EnumType value() default EnumType.STRING; | |
| 41 | -} |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +2 −59
| @@ -23,7 +23,6 @@ | ||
| 23 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 24 | 24 | |
| 25 | 25 | import com.datastax.driver.core.CCMBridge; |
| 26 | -import com.datastax.driver.core.ResultSet; | |
| 27 | 26 | import com.datastax.driver.core.Row; |
| 28 | 27 | import com.datastax.driver.core.utils.CassandraVersion; |
| 29 | 28 | import com.datastax.driver.mapping.annotations.*; |
| @@ -39,36 +38,6 @@ | ||
| 39 | 38 | ); |
| 40 | 39 | } |
| 41 | 40 | |
| 42 | - @Test(groups = "short") | |
| 43 | - public void should_allow_enum_as_int_in_accessor_params() { | |
| 44 | - UserAccessor accessor = new MappingManager(session) | |
| 45 | - .createAccessor(UserAccessor.class); | |
| 46 | - | |
| 47 | - accessor.addUser(0, Enum.FEMALE); | |
| 48 | - accessor.addUser(1, Enum.MALE); | |
| 49 | - | |
| 50 | - assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); | |
| 51 | - assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1); | |
| 52 | - | |
| 53 | - assertThat(accessor.getUser(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); | |
| 54 | - assertThat(accessor.getUser(Enum.FEMALE).one().getKey()).isEqualTo(0); | |
| 55 | - } | |
| 56 | - | |
| 57 | - @Test(groups = "short") | |
| 58 | - public void should_allow_enum_as_string_in_accessor_params() { | |
| 59 | - UserAccessor accessor = new MappingManager(session) | |
| 60 | - .createAccessor(UserAccessor.class); | |
| 61 | - | |
| 62 | - accessor.addUserStr(0, Enum.FEMALE); | |
| 63 | - accessor.addUserStr(1, Enum.MALE); | |
| 64 | - | |
| 65 | - assertThat(accessor.getUserStr(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); | |
| 66 | - assertThat(accessor.getUserStr(Enum.MALE).one().getKey()).isEqualTo(1); | |
| 67 | - | |
| 68 | - assertThat(accessor.getUserStr(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); | |
| 69 | - assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0); | |
| 70 | - } | |
| 71 | - | |
| 72 | 41 | @Test(groups = "short") |
| 73 | 42 | @CassandraVersion(major = 2.0, description = "Uses named parameters") |
| 74 | 43 | public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() { |
| @@ -118,21 +87,6 @@ | ||
| 118 | 87 | MALE, FEMALE |
| 119 | 88 | } |
| 120 | 89 | |
| 121 | - @Accessor | |
| 122 | - public interface UserAccessor { | |
| 123 | - @Query("select * from user where gender=?") | |
| 124 | - Result<User> getUser(@Enumerated(EnumType.ORDINAL) Enum value); | |
| 125 | - | |
| 126 | - @Query("select * from user_str where gender=?") | |
| 127 | - Result<UserStr> getUserStr(@Enumerated(EnumType.STRING) Enum value); | |
| 128 | - | |
| 129 | - @Query("insert into user (key, gender) values (?,?)") | |
| 130 | - ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value); | |
| 131 | - | |
| 132 | - @Query("insert into user_str (key, gender) values (?,?)") | |
| 133 | - ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value); | |
| 134 | - } | |
| 135 | - | |
| 136 | 90 | /** Tests various ways to match method parameters to query bind markers. */ |
| 137 | 91 | @Accessor |
| 138 | 92 | public interface UserPhoneAccessor { |
| @@ -176,15 +130,11 @@ | ||
| 176 | 130 | @PartitionKey |
| 177 | 131 | private int key; |
| 178 | 132 | |
| 179 | - @Enumerated(EnumType.ORDINAL) | |
| 180 | - private Enum gender; | |
| 181 | - | |
| 182 | 133 | public User() { |
| 183 | 134 | } |
| 184 | 135 | |
| 185 | - public User(int k, Enum val) { | |
| 136 | + public User(int k) { | |
| 186 | 137 | this.key = k; |
| 187 | - this.gender = val; | |
| 188 | 138 | } |
| 189 | 139 | |
| 190 | 140 | public int getKey() { |
| @@ -195,13 +145,6 @@ | ||
| 195 | 145 | this.key = pk; |
| 196 | 146 | } |
| 197 | 147 | |
| 198 | - public Enum getGender() { | |
| 199 | - return this.gender; | |
| 200 | - } | |
| 201 | - | |
| 202 | - public void setGender(Enum val) { | |
| 203 | - this.gender = val; | |
| 204 | - } | |
| 205 | 148 | } |
| 206 | 149 | |
| 207 | 150 | @Table(name = "user") |
| @@ -235,4 +178,4 @@ | ||
| 235 | 178 | this.gender = val; |
| 236 | 179 | } |
| 237 | 180 | } |
| 238 | -} | |
| No newline at the end of the file | ||
| 181 | +} |
deleted driver-mapping/src/test/java/com/datastax/driver/mapping/MapperEnumTest.java +0 −130
| @@ -1,130 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.mapping; | |
| 17 | - | |
| 18 | -import java.util.Collection; | |
| 19 | - | |
| 20 | -import com.google.common.collect.Lists; | |
| 21 | -import org.testng.annotations.Test; | |
| 22 | - | |
| 23 | -import static org.testng.Assert.assertNull; | |
| 24 | - | |
| 25 | -import com.datastax.driver.core.CCMBridge; | |
| 26 | -import com.datastax.driver.mapping.annotations.Enumerated; | |
| 27 | -import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 28 | -import com.datastax.driver.mapping.annotations.Table; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * Covers handling of enum fields by the mapper. | |
| 32 | - */ | |
| 33 | -public class MapperEnumTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 34 | - | |
| 35 | - @Override | |
| 36 | - protected Collection<String> getTableDefinitions() { | |
| 37 | - return Lists.newArrayList("CREATE TABLE asOrdinal (k int primary key, v int)", | |
| 38 | - "CREATE TABLE asString (k int primary key, v text)"); | |
| 39 | - } | |
| 40 | - | |
| 41 | - public enum Enum { | |
| 42 | - FOO, BAR | |
| 43 | - } | |
| 44 | - | |
| 45 | - @Test(groups = "short") | |
| 46 | - public void should_handle_null_enum_as_ordinal() { | |
| 47 | - Mapper<AsOrdinal> mapper = new MappingManager(session).mapper(AsOrdinal.class); | |
| 48 | - | |
| 49 | - mapper.save(new AsOrdinal(1, null)); | |
| 50 | - AsOrdinal o = mapper.get(1); | |
| 51 | - | |
| 52 | - assertNull(o.getV()); | |
| 53 | - } | |
| 54 | - | |
| 55 | - @Test(groups = "short") | |
| 56 | - public void should_handle_null_enum_as_string() { | |
| 57 | - Mapper<AsString> mapper = new MappingManager(session).mapper(AsString.class); | |
| 58 | - | |
| 59 | - mapper.save(new AsString(1, null)); | |
| 60 | - AsString o = mapper.get(1); | |
| 61 | - | |
| 62 | - assertNull(o.getV()); | |
| 63 | - } | |
| 64 | - | |
| 65 | - @Table(name = "asOrdinal") | |
| 66 | - public static class AsOrdinal { | |
| 67 | - @PartitionKey | |
| 68 | - private int k; | |
| 69 | - | |
| 70 | - @Enumerated(EnumType.ORDINAL) | |
| 71 | - private Enum v; | |
| 72 | - | |
| 73 | - public AsOrdinal(Integer k, Enum v) { | |
| 74 | - this.k = k; | |
| 75 | - this.v = v; | |
| 76 | - } | |
| 77 | - | |
| 78 | - public AsOrdinal() { | |
| 79 | - } | |
| 80 | - | |
| 81 | - public int getK() { | |
| 82 | - return k; | |
| 83 | - } | |
| 84 | - | |
| 85 | - public void setK(int k) { | |
| 86 | - this.k = k; | |
| 87 | - } | |
| 88 | - | |
| 89 | - public Enum getV() { | |
| 90 | - return v; | |
| 91 | - } | |
| 92 | - | |
| 93 | - public void setV(Enum v) { | |
| 94 | - this.v = v; | |
| 95 | - } | |
| 96 | - } | |
| 97 | - | |
| 98 | - @Table(name = "asString") | |
| 99 | - public static class AsString { | |
| 100 | - @PartitionKey | |
| 101 | - private int k; | |
| 102 | - | |
| 103 | - @Enumerated(EnumType.STRING) | |
| 104 | - private Enum v; | |
| 105 | - | |
| 106 | - public AsString(Integer k, Enum v) { | |
| 107 | - this.k = k; | |
| 108 | - this.v = v; | |
| 109 | - } | |
| 110 | - | |
| 111 | - public AsString() { | |
| 112 | - } | |
| 113 | - | |
| 114 | - public int getK() { | |
| 115 | - return k; | |
| 116 | - } | |
| 117 | - | |
| 118 | - public void setK(int k) { | |
| 119 | - this.k = k; | |
| 120 | - } | |
| 121 | - | |
| 122 | - public Enum getV() { | |
| 123 | - return v; | |
| 124 | - } | |
| 125 | - | |
| 126 | - public void setV(Enum v) { | |
| 127 | - this.v = v; | |
| 128 | - } | |
| 129 | - } | |
| 130 | -} |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +13 −41
| @@ -28,7 +28,6 @@ | ||
| 28 | 28 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | 29 | import static org.testng.Assert.*; |
| 30 | 30 | |
| 31 | -import com.datastax.driver.mapping.MapperTest.User.Gender; | |
| 32 | 31 | import com.datastax.driver.mapping.annotations.*; |
| 33 | 32 | import com.datastax.driver.core.*; |
| 34 | 33 | import com.datastax.driver.core.utils.UUIDs; |
| @@ -38,6 +37,7 @@ | ||
| 38 | 37 | */ |
| 39 | 38 | public class MapperTest extends CCMBridge.PerClassSingleNodeCluster { |
| 40 | 39 | |
| 40 | + @Override | |
| 41 | 41 | protected Collection<String> getTableDefinitions() { |
| 42 | 42 | // We'll allow to generate those create statement from the annotated entities later, but it's currently |
| 43 | 43 | // a TODO |
| @@ -56,6 +56,7 @@ | ||
| 56 | 56 | * |
| 57 | 57 | * And the next step will be to support UDT (which should be relatively simple). |
| 58 | 58 | */ |
| 59 | + @SuppressWarnings("unused") | |
| 59 | 60 | @Table(name = "users", |
| 60 | 61 | readConsistency="QUORUM", |
| 61 | 62 | writeConsistency="QUORUM") |
| @@ -64,8 +65,6 @@ | ||
| 64 | 65 | // Dummy constant to test that static fields are properly ignored |
| 65 | 66 | public static final int FOO = 1; |
| 66 | 67 | |
| 67 | - public enum Gender { FEMALE, MALE } | |
| 68 | - | |
| 69 | 68 | @PartitionKey |
| 70 | 69 | @Column(name = "user_id") |
| 71 | 70 | private UUID userId; |
| @@ -75,15 +74,12 @@ | ||
| 75 | 74 | @Column // not strictly required, but we want to check that the annotation works without a name |
| 76 | 75 | private int year; |
| 77 | 76 | |
| 78 | - private Gender gender; | |
| 79 | - | |
| 80 | 77 | public User() {} |
| 81 | 78 | |
| 82 | - public User(String name, String email, Gender gender) { | |
| 79 | + public User(String name, String email) { | |
| 83 | 80 | this.userId = UUIDs.random(); |
| 84 | 81 | this.name = name; |
| 85 | 82 | this.email = email; |
| 86 | - this.gender = gender; | |
| 87 | 83 | } |
| 88 | 84 | |
| 89 | 85 | public UUID getUserId() { |
| @@ -114,14 +110,6 @@ | ||
| 114 | 110 | return year; |
| 115 | 111 | } |
| 116 | 112 | |
| 117 | - public void setGender(Gender gender) { | |
| 118 | - this.gender = gender; | |
| 119 | - } | |
| 120 | - | |
| 121 | - public Gender getGender() { | |
| 122 | - return gender; | |
| 123 | - } | |
| 124 | - | |
| 125 | 113 | public void setYear(int year) { |
| 126 | 114 | this.year = year; |
| 127 | 115 | } |
| @@ -135,13 +123,12 @@ | ||
| 135 | 123 | return Objects.equal(userId, that.userId) |
| 136 | 124 | && Objects.equal(name, that.name) |
| 137 | 125 | && Objects.equal(email, that.email) |
| 138 | - && Objects.equal(year, that.year) | |
| 139 | - && Objects.equal(gender, that.gender); | |
| 126 | + && Objects.equal(year, that.year); | |
| 140 | 127 | } |
| 141 | 128 | |
| 142 | 129 | @Override |
| 143 | 130 | public int hashCode() { |
| 144 | - return Objects.hashCode(userId, name, email, year, gender); | |
| 131 | + return Objects.hashCode(userId, name, email, year); | |
| 145 | 132 | } |
| 146 | 133 | } |
| 147 | 134 | |
| @@ -151,6 +138,7 @@ | ||
| 151 | 138 | * the order must be specified (@ClusteringColumn(0), @ClusteringColumn(1), ...). |
| 152 | 139 | * The same stands for the @PartitionKey. |
| 153 | 140 | */ |
| 141 | + @SuppressWarnings("unused") | |
| 154 | 142 | @Table(name = "posts") |
| 155 | 143 | public static class Post { |
| 156 | 144 | |
| @@ -268,7 +256,7 @@ | ||
| 268 | 256 | // harcoded arg0, arg1, .... A big annoying, and apparently Java 8 will fix that |
| 269 | 257 | // somehow, but well, not a huge deal. |
| 270 | 258 | @Query("SELECT * FROM posts WHERE user_id=:userId AND post_id=:postId") |
| 271 | - public Post getOne(@Param("userId") UUID userId, | |
| 259 | + Post getOne(@Param("userId") UUID userId, | |
| 272 | 260 | @Param("postId") UUID postId); |
| 273 | 261 | |
| 274 | 262 | // Note that the following method will be asynchronous (it will use executeAsync |
| @@ -276,23 +264,16 @@ | ||
| 276 | 264 | // that we need to map the result to the Post entity thanks to the return type. |
| 277 | 265 | @Query("SELECT * FROM posts WHERE user_id=?") |
| 278 | 266 | @QueryParameters(consistency="QUORUM") |
| 279 | - public ListenableFuture<Result<Post>> getAllAsync(UUID userId); | |
| 267 | + ListenableFuture<Result<Post>> getAllAsync(UUID userId); | |
| 280 | 268 | |
| 281 | 269 | // The method above actually query stuff, but if a method is declared to return |
| 282 | 270 | // a Statement, it will not execute anything, but just return you the BoundStatement |
| 283 | 271 | // ready for execution. That way, you can batch stuff for instance (see usage below). |
| 284 | 272 | @Query("UPDATE posts SET content=? WHERE user_id=? AND post_id=?") |
| 285 | - public Statement updateContentQuery(String newContent, UUID userId, UUID postId); | |
| 273 | + Statement updateContentQuery(String newContent, UUID userId, UUID postId); | |
| 286 | 274 | |
| 287 | 275 | @Query("SELECT * FROM posts") |
| 288 | - public Result<Post> getAll(); | |
| 289 | - } | |
| 290 | - | |
| 291 | - @Accessor | |
| 292 | - public interface UserAccessor { | |
| 293 | - // Demonstrates use of an enum as an accessor parameter | |
| 294 | - @Query("UPDATE users SET name=?, gender=? WHERE user_id=?") | |
| 295 | - ResultSet updateNameAndGender(String name, Gender gender, UUID userId); | |
| 276 | + Result<Post> getAll(); | |
| 296 | 277 | } |
| 297 | 278 | |
| 298 | 279 | @Test(groups = "short") |
| @@ -302,7 +283,7 @@ | ||
| 302 | 283 | // supported by the Mapper object. |
| 303 | 284 | Mapper<User> m = new MappingManager(session).mapper(User.class); |
| 304 | 285 | |
| 305 | - User u1 = new User("Paul", "paul@yahoo.com", User.Gender.MALE); | |
| 286 | + User u1 = new User("Paul", "paul@yahoo.com"); | |
| 306 | 287 | u1.setYear(2014); |
| 307 | 288 | m.save(u1); |
| 308 | 289 | |
| @@ -320,7 +301,7 @@ | ||
| 320 | 301 | |
| 321 | 302 | Mapper<Post> m = manager.mapper(Post.class); |
| 322 | 303 | |
| 323 | - User u1 = new User("Paul", "paul@gmail.com", User.Gender.MALE); | |
| 304 | + User u1 = new User("Paul", "paul@gmail.com"); | |
| 324 | 305 | Post p1 = new Post(u1, "Something about mapping"); |
| 325 | 306 | Post p2 = new Post(u1, "Something else"); |
| 326 | 307 | Post p3 = new Post(u1, "Something more"); |
| @@ -372,15 +353,6 @@ | ||
| 372 | 353 | |
| 373 | 354 | assertTrue(postAccessor.getAllAsync(u1.getUserId()).get().isExhausted()); |
| 374 | 355 | |
| 375 | - // Pass an enum constant as an accessor parameter | |
| 376 | - UserAccessor userAccessor = manager.createAccessor(UserAccessor.class); | |
| 377 | - userAccessor.updateNameAndGender("Paule", User.Gender.FEMALE, u1.getUserId()); | |
| 378 | - Mapper<User> userMapper = manager.mapper(User.class); | |
| 379 | - assertEquals(userMapper.get(u1.getUserId()).getGender(), User.Gender.FEMALE); | |
| 380 | - | |
| 381 | - // Test that an enum value can be unassigned through an accessor (set to null). | |
| 382 | - userAccessor.updateNameAndGender("Paule", null, u1.getUserId()); | |
| 383 | - assertEquals(userMapper.get(u1.getUserId()).getGender(), null); | |
| 384 | 356 | } |
| 385 | 357 | |
| 386 | 358 | @Test(groups="short") |
| @@ -390,7 +362,7 @@ | ||
| 390 | 362 | Mapper<Post> m = manager.mapper(Post.class); |
| 391 | 363 | |
| 392 | 364 | // Insert a few posts |
| 393 | - User u1 = new User("Paul", "paul@gmail.com", User.Gender.MALE); | |
| 365 | + User u1 = new User("Paul", "paul@gmail.com"); | |
| 394 | 366 | Post p1 = new Post(u1, "Something about mapping"); |
| 395 | 367 | Post p2 = new Post(u1, "Something else"); |
| 396 | 368 | Post p3 = new Post(u1, "Something more"); |
modified features/object_mapper/creating/README.md +0 −25
| @@ -82,31 +82,6 @@ | ||
| 82 | 82 | [cc]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/mapping/annotations/ClusteringColumn.html |
| 83 | 83 | [pks]:http://thelastpickle.com/blog/2013/01/11/primary-keys-in-cql.html |
| 84 | 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/3.0/com/datastax/driver/mapping/annotations/Enumerated.html | |
| 109 | - | |
| 110 | 85 | #### Computed fields |
| 111 | 86 | |
| 112 | 87 | [@Computed][computed] can be used on fields that are the result of a |
modified upgrade_guide/README.md +22 −0
| @@ -208,6 +208,28 @@ | ||
| 208 | 208 | 20. `TableMetadata.Options` has been made a top-level class and renamed to |
| 209 | 209 | `TableOptionsMetadata`. It is now also used by `MaterializedViewMetadata`. |
| 210 | 210 | |
| 211 | +21. The mapper annotation `@Enumerated` has been removed, users should now | |
| 212 | + use the newly-introduced `driver-extras` module to get automatic | |
| 213 | + enum-to-CQL mappings. Two new codecs provide the same functionality: | |
| 214 | + `EnumOrdinalCodec` and `EnumNameCodec`: | |
| 215 | + | |
| 216 | + ```java | |
| 217 | + enum Foo {...} | |
| 218 | + enum Bar {...} | |
| 219 | + | |
| 220 | + // register the appropriate codecs | |
| 221 | + CodecRegistry.DEFAULT_INSTANCE | |
| 222 | + .register(new EnumOrdinalCodec<Foo>(Foo.class)) | |
| 223 | + .register(new EnumNameCodec<Bar>(Bar.class)) | |
| 224 | + | |
| 225 | + // the following mappings are handled out-of-the-box | |
| 226 | + @Table | |
| 227 | + public class MyPojo { | |
| 228 | + private Foo foo; | |
| 229 | + private List<Bar> bars; | |
| 230 | + ... | |
| 231 | + } | |
| 232 | + ``` | |
| 211 | 233 | |
| 212 | 234 | ### 2.1.8 |
| 213 | 235 |