Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA Redis : Cannot write custom method based query

I have configured Spring Data JPA with Redis and using RedisRepositories with provides methods like find(), findAll() etc. All these methods seem to be working just fine, but I am not able to write my custom method like.

RedisEntity findByGenderAndGrade(String gender, String grade);

RedisEntity is a simple POJO Entity class. If you want any more info, please let me know in messages.

Following is my entity:

@Data
@RedisHash("test1")
public class RedisEntity implements Serializable {

    @Id
    @GeneratedValue
    private String id;
    private String name;
    private String gender;
    private Integer grade;
}

Repository:

@Repository
public interface TestRepository extends JpaRepository<RedisEntity, String> {

    List<RedisEntity> findAllByGender(String gender);
    List<RedisEntity> findAllByGrade(Integer grade);
}

Service/Controller:

        @Override
        public List<RedisEntity> getById(String id) {
            return testRepository.findById(id); //returns data perfectly.
        }
        @Override
        public List<RedisEntity> getAllByGender(String gender) {
            return testRepository.findAllByGender(gender); //returns [] 
        }

        @Override
        public void saveEntity(RedisEntity redisEntity) {
            testRepository.save(redisEntity); // saves it in redis perfectly.
        }

Also, findByGender and findAllByGender both give [], although I can see data in my redis database and save it as well.

As requested by FrançoisDupire,

@Configuration
public class RedisConfig {

    @Autowired
    private DeploymentProperties deploymentProperties;

    private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
        redisStandaloneConfiguration.setPassword(RedisPassword.of("root"));
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

Also, I had referred this article: Baeldung article on Spring data redis

like image 290
Ayush Nigam Avatar asked Oct 28 '22 09:10

Ayush Nigam


1 Answers

As mentioned by @JoshJ and verified by myself and others, The solution to the problem is:

Adding @Indexed annotation to all those columns/fields which need to be used with all finds.

@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {

 @Id
 private String employeeId;

 private String firstName;

 private String lastName;

 @Indexed
 private String gender;

 @Indexed
 private String grade;

}
like image 127
Ayush Nigam Avatar answered Nov 15 '22 10:11

Ayush Nigam