gpt4 book ai didi

java - Spring @RedisHash findAll() 返回空值

转载 作者:行者123 更新时间:2023-12-01 16:14:55 26 4
gpt4 key购买 nike

我使用 Redis 来存储具有实体的学生:

@RedisHash("Student")
public class Student implements Serializable {
@Id
private Long id;
@Indexed
private String name;
private Integer age;
// getters
// setters
// Constructor with full parameters
}

和存储库:

@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
}

我可以将学生列表保存到 Redis 数据库并获取该列表,不会出现任何错误:

@Autowired
StudentRepository repo;

List<Student> students = new ArrayList<>();
Student student1 = new Student(........);
students.add(student1);
Student student2 = new Student(........);
students.add(student2);
repo.findAll().forEach(){
System.out.println(student);
}

问题是当我的其他项目(我正在使用微服务架构构建应用程序)时,我使用 findAll() 函数来获取学生列表,它返回两个列表null 元素。如果我使用 findByName(String name),它仍然返回所需的结果。

有谁曾经遇到过这个问题可以帮助我吗,先谢谢你了?

最佳答案

原来我在另一个项目上的 Student 类具有相同的 @RedisHash("Student") 但不同的完整类名(相同的类名但不同的包)。我认为这个问题属于图书馆。

更新:由于@RedisHash无法按预期工作,我找到了方法:即添加@TypeAlias("Student") 到实体 Student,因此您可以将 Student 放置在源代码中的任何位置

关于java - Spring @RedisHash findAll() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62427392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com