Allow document ID to be other than String (#1534)
* fixes #1529 * add author
This commit is contained in:
@@ -81,6 +81,7 @@ import org.springframework.util.CollectionUtils;
|
||||
* @author Geoffrey Mina
|
||||
* @author Mark Paluch
|
||||
* @author Michael Reiche
|
||||
* @author Remi Bleuse
|
||||
*/
|
||||
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware {
|
||||
|
||||
@@ -955,7 +956,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
|
||||
Object value = expression != null ? evaluator.evaluate(expression) : source.get(property.getFieldName());
|
||||
|
||||
if (property == entity.getIdProperty() && parent == null) {
|
||||
return (R) source.getId();
|
||||
return readValue(source.getId(), property.getTypeInformation(), source);
|
||||
}
|
||||
if (value == null) {
|
||||
return null;
|
||||
|
||||
@@ -686,6 +686,25 @@ public class MappingCouchbaseConverterTests {
|
||||
assertThat(read.deleted.truncatedTo(ChronoUnit.MILLIS)).isEqualTo(deleted.truncatedTo(ChronoUnit.MILLIS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesAndReadsIdAsUUID() {
|
||||
String idAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d123";
|
||||
UUID id = UUID.fromString(idAsString);
|
||||
String otherAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d456";
|
||||
UUID other = UUID.fromString(otherAsString);
|
||||
|
||||
UuidIdEntity entity = new UuidIdEntity(id, other);
|
||||
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertThat(converted.getContent().get("other")).isEqualTo(otherAsString);
|
||||
assertThat(converted.getId()).isEqualTo(idAsString);
|
||||
|
||||
UuidIdEntity read = converter.read(UuidIdEntity.class, converted);
|
||||
assertThat(read.id).isEqualTo(id);
|
||||
assertThat(read.other).isEqualTo(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesAndReadsNestedClass() {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
@@ -946,6 +965,16 @@ public class MappingCouchbaseConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class UuidIdEntity {
|
||||
@Id private UUID id;
|
||||
private UUID other;
|
||||
|
||||
public UuidIdEntity(UUID id, UUID other) {
|
||||
this.id = id;
|
||||
this.other = other;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void idNotGenerated() {
|
||||
class Entity {
|
||||
|
||||
Reference in New Issue
Block a user