spring-projects / spring-projects/spring-data-redis

Redis repository: Error while converting to Timestamp

Open
#2,898 3 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Apr 29, 2024.

status: waiting-for-triage
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Using Spring data Redis 3.2.5 and repository,
Given this structure

@RedisHash("Fruit")
public class Fruit {
    @Id
    private Integer id;
    private String name;
    private Timestamp createdAt;
}

And

public interface FruitRedisRepository extends CrudRepository<Fruit, Integer> {}
@Configuration
public class RedisConfig {
    @Bean
    public RedisConnectionFactory redisConnectionFactory()
    {
        return new LettuceConnectionFactory("localhost", 16379);
    }
}

This test fails and throws exception:

@SpringBootTest
class TestSpringRedisApplicationTests {

    @Autowired
    FruitRedisRepository fruitRedisRepository;

    @Test
    void test() {
        //creating sample fruit
        Fruit fruit = new Fruit(1, "banana", Timestamp.from(Instant.now()));

        //fruit is saved into redis with no problem
        assertThat(fruitRedisRepository.save(fruit)).isNotNull();

        //this should work fine but throws exception because of the timestamp
        assertThat(fruitRedisRepository.findById(1)).isNotNull();
    }
}

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [java.sql.Timestamp]

at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:294)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:185)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:165)
at org.springframework.data.redis.core.convert.MappingRedisConverter.fromBytes(MappingRedisConverter.java:998)
at org.springframework.data.redis.core.convert.MappingRedisConverter.readProperty(MappingRedisConverter.java:329)
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$doReadInternal$0(MappingRedisConverter.java:245)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:298)
at org.springframework.data.redis.core.convert.MappingRedisConverter.doReadInternal(MappingRedisConverter.java:237)
at org.springframework.data.redis.core.convert.MappingRedisConverter.read(MappingRedisConverter.java:183)
at org.springframework.data.redis.core.convert.MappingRedisConverter.read(MappingRedisConverter.java:114)
at org.springframework.data.redis.core.RedisKeyValueAdapter.get(RedisKeyValueAdapter.java:290)
at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$findById$3(KeyValueTemplate.java:241)
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:314)
at org.springframework.data.keyvalue.core.KeyValueTemplate.findById(KeyValueTemplate.java:239)
at org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository.findById(SimpleKeyValueRepository.java:98)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:354)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:277)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:516)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:628)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:168)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:223)
at jdk.proxy2/jdk.proxy2.$Proxy59.findById(Unknown Source)
at ir.co.rrr.testspringredis.TestSpringRedisApplicationTests.test(TestSpringRedisApplicationTests.java:30)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

But the timestamp is saved correctly in redis:
hmget Fruit:1 id name createdAt

  1. "1"
  2. "banana"
  3. "1713704899797"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.