为什么spring data redis findById returns Optional升级到2.3版本后空值2.RELEASE
Why spring data redis findById returns null values in Optional after upgrading to version 2.3.2.RELEASE
spring 存储库 findById() 在 spring-data-redis 2.3 版上运行良好。1.RELEASE
它在 spring-data-redis 版本 2.3 上失败。2.RELEASE
这里是 link 示例存储库,在 pom.xml 文件中编辑版本,然后 运行,然后查看问题
https://github.com/mnguyencntt/spring-data-redis-optional
我的逻辑代码很简单:
如果找到 studentId,return 存在 RedisStudent 对象。
否则创建新的 RedisStudent 并存储在 Redis 中,return 新的 RedisStudent 对象。
RedisInfoController.java
final Optional<RedisStudent> redisExisting = redisStudentRepository.findById(studentId);
if (redisExisting.isPresent()) {
// Spring boot 2.3.2 will print out: RedisStudent(id=null, name=null, age=null, creationTime=null)
// Spring boot 2.3.1 will print out: RedisStudent(id=12345, name=Minh, age=28, creationTime=2020-07-28T21:31:18.318)
log.info("{}", redisExisting.get());
return redisExisting.get();
}
// Spring boot 2.3.1 will print out: Optional.empty
log.info("{}", redisExisting);
RedisStudent student = new RedisStudent();
student.setId(studentId);
student.setName("Minh");
student.setAge("28");
student.setCreationTime(LocalDateTime.now());
return redisStudentRepository.save(student);
可能与您的控制器中的 studentId 为空有关?
您没有在请求参数中使用 studentId。
您 运行 进入 DATAREDIS-1191. It will be fixed in the 2.3.3.RELEASE。
spring 存储库 findById() 在 spring-data-redis 2.3 版上运行良好。1.RELEASE
它在 spring-data-redis 版本 2.3 上失败。2.RELEASE
这里是 link 示例存储库,在 pom.xml 文件中编辑版本,然后 运行,然后查看问题
https://github.com/mnguyencntt/spring-data-redis-optional
我的逻辑代码很简单:
如果找到 studentId,return 存在 RedisStudent 对象。
否则创建新的 RedisStudent 并存储在 Redis 中,return 新的 RedisStudent 对象。
RedisInfoController.java
final Optional<RedisStudent> redisExisting = redisStudentRepository.findById(studentId);
if (redisExisting.isPresent()) {
// Spring boot 2.3.2 will print out: RedisStudent(id=null, name=null, age=null, creationTime=null)
// Spring boot 2.3.1 will print out: RedisStudent(id=12345, name=Minh, age=28, creationTime=2020-07-28T21:31:18.318)
log.info("{}", redisExisting.get());
return redisExisting.get();
}
// Spring boot 2.3.1 will print out: Optional.empty
log.info("{}", redisExisting);
RedisStudent student = new RedisStudent();
student.setId(studentId);
student.setName("Minh");
student.setAge("28");
student.setCreationTime(LocalDateTime.now());
return redisStudentRepository.save(student);
可能与您的控制器中的 studentId 为空有关?
您没有在请求参数中使用 studentId。
您 运行 进入 DATAREDIS-1191. It will be fixed in the 2.3.3.RELEASE。