Spring Data Redis升级到版本2.3.2.RELEASE后,findById返回Optional中的null值。

huangapple go评论58阅读模式
英文:

Why spring data redis findById returns null values in Optional after upgrading to version 2.3.2.RELEASE

问题

spring repository findById() 在 spring-data-redis 版本 2.3.1.RELEASE 上正常工作。

在 spring-data-redis 版本 2.3.2.RELEASE 上失败。


以下是示例仓库链接,请编辑 pom.xml 文件中的版本,然后运行,然后查看问题:

https://github.com/mnguyencntt/spring-data-redis-optional


我的逻辑代码非常简单:

如果找到 studentId,则返回现有的 RedisStudent 对象。

否则,创建新的 RedisStudent 并存储在 Redis 中,返回新的 RedisStudent 对象。


RedisInfoController.java

final Optional redisExisting = redisStudentRepository.findById(studentId);
如果 redisExisting 存在:

// Spring boot 2.3.2 将打印出:RedisStudent(id=null, name=null, age=null, creationTime=null)
// Spring boot 2.3.1 将打印出:RedisStudent(id=12345, name=Minh, age=28, creationTime=2020-07-28T21:31:18.318)
log.info("{}", redisExisting.get());
return redisExisting.get();

如果 redisExisting 为空:

// Spring boot 2.3.1 将打印出: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);

英文:

spring repository findById() is working fine on spring-data-redis version 2.3.1.RELEASE

its failed on spring-data-redis version 2.3.2.RELEASE


Here is link to sample repository, edit version in pom.xml file, then run, then see the issue

https://github.com/mnguyencntt/spring-data-redis-optional


My logic code is very simple:

if studentId found, return existing RedisStudent object.

else create new RedisStudent & store in Redis, return new RedisStudent object.


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);

答案1

得分: 2

你遇到了 DATAREDIS-1191。这将在 2.3.3.RELEASE 版本中修复

英文:

You are running into DATAREDIS-1191. It will be fixed in the 2.3.3.RELEASE.

答案2

得分: -2

也许与您的控制器中studentId为空有关?

您没有在请求参数中包含studentId。

英文:

Maybe it has something to do the fact that studentId is null in your controller?

You are not taking studentId in the request param.

huangapple
  • 本文由 发表于 2020年7月28日 18:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63132068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定