【发布时间】:2018-01-07 05:18:27
【问题描述】:
我有一个如下的 java 对象,我存储在 redis 存储中。
@RedisHash("UserProfile")
public class UserProfile implements Serializable {
@Id String id;
@Reference PersonalInfo personalInfo = new PersonalInfo();
@Reference BusinessInfo businessInfo = new BusinessInfo();
...
}
现在,PersonalInfo 对象的结构如下:
public class PersonalInfo {
private String firstName;
private String lastName;
@Indexed private String userId;
private ContactInfo contactInfo = new ContactInfo();
}
请注意,PersonalInfo 对象中的 userId 已编入索引。
我的目标是使用 spring 数据通过用户 ID 查询 redis 中的 UserProfile 对象。
CrudRepository 接口将无法直接执行findByUserId(),除非我在 UserProfile 对象中有 userId。
如果有更好的方法来编写查询以通过 userid 查找用户,我不想将 userId 直接包含在 UserProfile 对象中。
对我如何做这件事有什么建议吗?
更新: 我将如下方法添加到存储库:
public interface UserProfileRepository extends CrudRepository<UserProfile, String> {
List<UserProfile> findByPersonalInfo_UserId(String userId);
}
但是在代码中调用此方法时,我收到以下错误。不确定我正在使用的 redis/spring 库是否存在问题。我正在使用
- springboot-starter-parent 版本:1.5.2.RELEASE
- spring-data-redis 版本:1.8.6.RELEASE
例外:
java.lang.NoSuchMethodError: org.springframework.data.keyvalue.core.query.KeyValueQuery.getCriteria()Ljava/lang/Object;
at org.springframework.data.redis.repository.query.RedisQueryCreator.complete(RedisQueryCreator.java:101) ~[spring-data-redis-1.8.6.RELEASE.jar:na]
at org.springframework.data.redis.repository.query.RedisQueryCreator.complete(RedisQueryCreator.java:41) ~[spring-data-redis-1.8.6.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.createQuery(KeyValuePartTreeQuery.java:184) ~[spring-data-keyvalue-1.2.1.RELEASE.jar:na]
at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.prepareQuery(KeyValuePartTreeQuery.java:128) ~[spring-data-keyvalue-1.2.1.RELEASE.jar:na]
at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.execute(KeyValuePartTreeQuery.java:87) ~[spring-data-keyvalue-1.2.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at com.sun.proxy.$Proxy66.findByPersonalInfo_UserId(Unknown Source) ~[na:na]
更新 2: 我可以通过将 springboot 版本更改为 1.5.6.RELEASE 来修复上述异常。
但是,现在我面临的问题是,当我通过 userid 查询时,即使有匹配项,它也没有给我 UserProfiles。
例如redis-cli显示:
KEYS *
UserProfile:d0876c1f-5684-4ee4-acbb-7a92b7fa25ae
UserProfile:3591e476-29d7-4c3c-a7e5-6272231f96e0
UserProfile:3591e476-29d7-4c3c-a7e5-6272231f96e0:idx
UserProfile:d0876c1f-5684-4ee4-acbb-7a92b7fa25ae:idx
UserProfile:51814a77-bf40-4912-b700-cfa50d1c4b25
UserProfile:personalInfo.userId:adrian.tremblay
UserProfile:66ba8276-1bb0-47a0-a54d-4c9d99b8bf80
UserProfile:66ba8276-1bb0-47a0-a54d-4c9d99b8bf80:idx
UserProfile:personalInfo.userId:anthony.turner
UserProfile:personalInfo.userId:ashleigh.hayes
UserProfile:a81356b0-27ef-4a34-92a3-629be5114f0e
UserProfile
当我执行 findAll、findById 时,它会返回结果。当我执行 findByPersonalInfo_UserId() 并传递诸如 anthony.turner 或 ashleigh.hayes 之类的值时,什么也没有出现。我是否以某种不正确的格式提供了 userId?我尝试了一些不同的方法,但没有奏效。
更新 3:我还尝试删除 UserProfile 对象中的 @Reference(以便 redis 对象变平,而不是指向引用)。但这也无济于事。
【问题讨论】:
-
你得到正确的unsver
标签: java redis spring-data spring-data-jpa spring-data-redis