[英]FindBy in Redis Repository with spring
I want save and get data from redis use spring.我想使用 spring 从 redis 保存和获取数据。 I have redis only for profile
SOME
.我有 redis 仅用于配置文件
SOME
。 I save data ok.我保存数据ok。 But when I try to get data by method
findByIdAndName
, I always have empty Optional.但是当我尝试通过方法
findByIdAndName
获取数据时,我总是有空的 Optional。
My pojo:我的pojo:
@RedisHash("Some")
@Data
@AllArgsConstructor
public class SomeInfo implements Serializable {
@Id
private String id;
@Indexed
private String name;
@TimeToLive(unit = TimeUnit.DAYS)
private Long expiration;
}
My repository:我的存储库:
public interface SomeInfoRepository extends CrudRepository<SomeInfo, String> {
Optional<SomeInfo> findByIdAndName(String id, String name);
}
And I have redis config:我有 redis 配置:
@Profile(Profiles.SOME)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RedisProperties.class)
@EnableRedisRepositories(basePackageClasses = { SomeInfoRepository.class })
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
return template;
}
@Bean(name = "someTopic")
public ChannelTopic someTopic(@Value("${app.topics.some}") String someChannel) {
return new ChannelTopic(someChannel);
}
@Bean
public RedisMessageListenerContainer roundRedisContainer(RedisConnectionFactory connectionFactory,
SomeMessageListener someMessageListener,
@Qualifier("someTopic") ChannelTopic someTopic) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(someMessageListener, someTopic);
return container;
}
}
And application.yml和 application.yml
spring:
redis:
host: localhost
port: 6379
ssl: false
password: some
data.redis.repositories.enabled: false
I know that data save ok:我知道数据保存正常:
keys *
1) "Some:36:idx"
2) "Some"
3) "Some:name:af0b83a2b15f018bdd404aef33bfb11f"
4) "Some:name:f5f8c3a9119bb822b0c2fde3265d37fa"
5) "Some:41:idx"
6) "Some:41:phantom"
7) "Some:36:phantom"
8) "Some:41"
9) "Some:36"
And和
hgetall Some:41
1) "_class"
2) "ru.some.SomeInfo"
3) "id"
4) "41"
5) "name"
6) "af0b83a2b15f018bdd404aef33bfb11f"
7) "expiration"
8) "30"
So, why is my method findByIdAndName
not work?那么,为什么我的方法
findByIdAndName
不起作用? If I use method findById(id)
, it work correct and return data.如果我使用方法
findById(id)
,它可以正常工作并返回数据。
You're missing the @Indexed
annotation on the id
field.您缺少
id
字段上的@Indexed
注释。
Also see this question for reference: Unable to get result from the Redis using Crud Repository in Spring Boot?另请参阅此问题以供参考: Unable to get result from the Redis using Crud Repository in Spring Boot?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.