1

I would like to store my entities in Redis with a UUID key:

@RedisHash("order")
public class Order {

    @Id
    private UUID id;

    ...
}

However, I get the following exception with this setup:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.UUID] to type [byte[]]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.toBytes(MappingRedisConverter.java:948) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$writeInternal$2(MappingRedisConverter.java:592) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:328) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.writeInternal(MappingRedisConverter.java:584) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:396) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:122) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.RedisKeyValueAdapter.put(RedisKeyValueAdapter.java:208) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$update$1(KeyValueTemplate.java:204) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:343) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.update(KeyValueTemplate.java:204) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository.save(SimpleKeyValueRepository.java:103) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
...

When using String instead of UUID, no such exception appears.

How can I use UUID as ID type?

nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95

1 Answers1

1

EDIT:

To be more precise you have to register a CustomConversion bean in your context with the name redisCustomConversions. See this post: Redis - How to configure custom conversions

So in your case it would be something like

@Bean
public CustomConversion redisCustomConversions(){
    return new CustomConversions(
        Arrays.asList(new UUIDToStringConverter(), new StringToUUIDConverter()))))
}

Original:

I think the easiest way to fix this is to write a type converter for uuid. Something like a

class UUIDConverter implements Converter<UUID, String>

Or when needed

class UUIDConverter implements Converter<UUID, byte[]>

You have to register this as a bean in your context.

Jan Schumacher
  • 309
  • 2
  • 11
  • Unfortunately I still see the exception after I register this converter. – nagy.zsolt.hun Apr 17 '18 at 07:05
  • However, your answer led me to the correct solution: https://stackoverflow.com/questions/42915576/redis-how-to-configure-custom-conversions. We need to register a RedisCustomConversions bean. If you update your answer with this information, I will accept it. Thanks. – nagy.zsolt.hun Apr 17 '18 at 07:17