Spring Data JPA Redis:无法编写基于自定义方法的查询
Spring Data JPA Redis : Cannot write custom method based query
我已经使用 Redis 配置了 Spring Data JPA,并使用 RedisRepositories
提供了 find()
、findAll()
等方法。所有这些方法似乎都运行良好,但我无法像这样编写自定义方法。
RedisEntity findByGenderAndGrade(String gender, String grade);
RedisEntity 是一个简单的 POJO 实体 class。如果您想了解更多信息,请在消息中告诉我。
以下是我的实体:
@Data
@RedisHash("test1")
public class RedisEntity implements Serializable {
@Id
@GeneratedValue
private String id;
private String name;
private String gender;
private Integer grade;
}
存储库:
@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.
}
此外,
findByGender 和 findAllByGender 都给出 [],虽然我可以在我的 redis 数据库中看到数据并保存它。
应 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;
}
}
另外,我参考了这篇文章:Baeldung article on Spring data redis
我们有 Spring Data Redis 库,它提供了编写自定义 method.Attaching 示例代码的范围。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
实体定义
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
private String gender;
private String grade;
}
存储库定义
@Repository
public interface RedisEntityRepository extends CrudRepository<RedisEntity, String>{
List<RedisEntity> findAllByGenderAndGrade(String gender, String grade);
}
实施
@Component
public class RedisEntityImpl implements RedisEntityService {
@Autowired
private RedisEntityRepository redisEntityRepository;
@Override
public List<RedisEntity> getAllByGenderAndGrade(String gender, String grade) {
return redisEntityRepository.findAllByGenderAndGrade(gender,grade);
}
}
属性
spring.cache.type = redis
spring.redis.host = localhost
spring.redis.port = 6379
正如@JoshJ 所提到的,并经过我自己和其他人的验证,
问题的解决方案是:
添加@Indexed 注释
对于所有需要与所有发现一起使用的 columns/fields。
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
@Indexed
private String gender;
@Indexed
private String grade;
}
我已经使用 Redis 配置了 Spring Data JPA,并使用 RedisRepositories
提供了 find()
、findAll()
等方法。所有这些方法似乎都运行良好,但我无法像这样编写自定义方法。
RedisEntity findByGenderAndGrade(String gender, String grade);
RedisEntity 是一个简单的 POJO 实体 class。如果您想了解更多信息,请在消息中告诉我。
以下是我的实体:
@Data
@RedisHash("test1")
public class RedisEntity implements Serializable {
@Id
@GeneratedValue
private String id;
private String name;
private String gender;
private Integer grade;
}
存储库:
@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.
}
此外, findByGender 和 findAllByGender 都给出 [],虽然我可以在我的 redis 数据库中看到数据并保存它。
应 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;
}
}
另外,我参考了这篇文章:Baeldung article on Spring data redis
我们有 Spring Data Redis 库,它提供了编写自定义 method.Attaching 示例代码的范围。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
实体定义
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
private String gender;
private String grade;
}
存储库定义
@Repository
public interface RedisEntityRepository extends CrudRepository<RedisEntity, String>{
List<RedisEntity> findAllByGenderAndGrade(String gender, String grade);
}
实施
@Component
public class RedisEntityImpl implements RedisEntityService {
@Autowired
private RedisEntityRepository redisEntityRepository;
@Override
public List<RedisEntity> getAllByGenderAndGrade(String gender, String grade) {
return redisEntityRepository.findAllByGenderAndGrade(gender,grade);
}
}
属性
spring.cache.type = redis
spring.redis.host = localhost
spring.redis.port = 6379
正如@JoshJ 所提到的,并经过我自己和其他人的验证, 问题的解决方案是:
添加@Indexed 注释 对于所有需要与所有发现一起使用的 columns/fields。
@Data
@RedisHash("EmployeeDetails")
public class RedisEntity {
@Id
private String employeeId;
private String firstName;
private String lastName;
@Indexed
private String gender;
@Indexed
private String grade;
}