Spring Data Redis composite key

Viewed 961

I need to store the following class in Redis:

@Data
@Builder
@Accessors(chain = true)
@RedisHash("Meter")
public class Meter {
    @org.springframework.data.annotation.Id
    private Id id;
    private String accountId;
    private String name;
    private Tags tags;
    private Type type;

    @Getter
    @RequiredArgsConstructor
    public static class Id {
        private final String name;
        private final String accountId;

        @Override
        public String toString() {
            return name + ":" + accountId;
        }

        public static Id of(String name, String accountId) {
            return new Id(name, accountId);
        }
    }
}

As you can see, it has a custom id class. The idea is that this custom key class will be represented in Redis as name:accountId. I also have a repository for this class:

public interface MeterRepository extends CrudRepository<Meter, Meter.Id> {
}

When I run the project and try to use MeterRepository, I get No converter found capable of converting from type [...entity.Meter$Id] to type [java.lang.String], though I created a converter and registered it:

@Component
public class MeterIdToStringConverter implements Converter<Meter.Id, String> {
    @Override
    public String convert(Meter.Id meterId) {
        return meterId.toString();
    }
}
@Configuration
@RequiredArgsConstructor
public class ConversionConfiguration {

    private final MeterIdToStringConverter meterIdToStringConverter;

    @Bean
    public ConversionService conversionService(List<Converter> converters) {
        final DefaultConversionService conversionService = new DefaultConversionService();
        converters.forEach(conversionService::addConverter);
        conversionService.addConverter(meterIdToStringConverter);
        return conversionService;
    }
}

Moreover, I tried autowiring ConversionService and calling conversionService.canConvert(Meter.Id.class, String.class) - it returned true.

What am I doing wrong?

0 Answers
Related