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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With