ktcl
ktcl

Reputation: 413

Spring @RedisHash findAll() return null values

I'm using Redis to store students with entity:

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

and repository:

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

I can save a list of students to Redis database and get that list without any error:

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

The problem is when other project of mine (I'm building apps with micro-service architecture), I use findAll() function to get that list of students, it returns a list of two null elements. If I use findByName(String name), it still returns desired result.

Anyone who used to face this problem can help me, thank you in advanced ?

Upvotes: 3

Views: 2536

Answers (1)

ktcl
ktcl

Reputation: 413

Turns out my Student class on the other project has the same @RedisHash("Student") but different full class name (same class name but different package). I think this issue belongs to the library.

Updated: cause @RedisHash doesn't work like expected, I found the way: that is adding @TypeAlias("Student") to the entity Student, therefore you can place Student anywhere in your source code

Upvotes: 4

Related Questions