java - 由 : org. springframework.data.mapping.PropertyReferenceException: No property findAll found for type User - Redis 引起

标签 java spring-boot redis jedis spring-data-redis

我是 Redis 的新手并使用 Spring Boot + Spring Data Redis例子。在这个例子中,我使用了 QueryByExampleRedisExecutor<T>在存储库方法上并使用了 Example执行自定义查询的 API。

以下是 Redis NoSQL DB 中存在的 KEYS。

redis 127.0.0.1:6379> KEYS *
 1) "country:76c78bcc-bb2a-41b3-a1fc-3dbb3042edd6:idx"
 2) "country:76c78bcc-bb2a-41b3-a1fc-3dbb3042edd6"
 3) "user:lastName:Kerr"
 4) "user"
 5) "user:role.roleName:API"
 6) "country:countryName:India"
 7) "Student:name:John Doe"
 8) "user:8252a4b3-22a1-4e6c-99fc-04fed93a21a5:idx"
 9) "user:8252a4b3-22a1-4e6c-99fc-04fed93a21a5"
10) "user:middleName:Lima"
11) "Student"
12) "user:middleName:Mike"
13) "user:firstName:John"
14) "country:countryCode:+91"
15) "user:role.roleName:ADMIN"
16) "user:lastName:Wixson"
17) "Student:1:idx"
18) "user:46ca0606-38a5-4d0c-8cea-38e2c4fa5bfa:idx"
19) "Student:2:idx"
20) "Student:1"
21) "user:firstName:Matt"
22) "user:46ca0606-38a5-4d0c-8cea-38e2c4fa5bfa"
23) "Student:2"
24) "country"
25) "Student:name:Michael Harford"
26) "country:84fc82f2-1c21-4d3e-9429-f1ee1039ceed:idx"
27) "country:84fc82f2-1c21-4d3e-9429-f1ee1039ceed"
redis 127.0.0.1:6379>

存储库方法 -

Example<User> example = Example.of(User.builder().firstName("Mike").build());
long count = userRepository.count();
System.out.println(count);

当我执行以下方法时,我原以为只会得到一个结果,但我得到了两个计数。为什么?

此处显示更多信息:Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

我们怎样才能看到 Redis snippet在日志上?我需要设置什么配置?

public interface UserRepository extends CrudRepository<User, String>, QueryByExampleExecutor<User>{
    List<User> findByFirstName(String firstName);

    List<User> findByFirstNameAndLastName(String firstName, String lastName);

    @Query("SELECT u FROM User u WHERE u.middleName LIKE :middleName ")
    List<User> findCustomByMiddleName(@Param("middleName") String middleName); 

    List<User> findByRole_RoleName(String roleName);
}

以下错误供引用 -

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
    at com.baeldung.MainAppDemo.main(MainAppDemo.java:31)
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:334)
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:287)
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(Unknown Source)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:269)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:252)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(Unknown Source)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:380)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(Unknown Source)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:381)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:93)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.createQuery(KeyValuePartTreeQuery.java:206)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.prepareQuery(KeyValuePartTreeQuery.java:148)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.execute(KeyValuePartTreeQuery.java:106)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:602)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:590)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy69.findAll(Unknown Source)
    at com.baeldung.MainAppDemo.run(MainAppDemo.java:47)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792)
    ... 5 common frames omitted

最佳答案

您正在构造一个 Example,但随后由于未使用它而将其丢弃。您正在调用 userRepository.count();,它基本上计算所有记录。您应该使用采用Examplecount

这个方法在QueryByExampleExecutor您的界面 UserRepository 也应该扩展。然后你可以简单地做。

Example<User> example = Example.of(User.builder().firstName("Mike").build());
long count = userRepository.count(example);
System.out.println(count);

您应该会得到预期的结果。

关于java - 由 : org. springframework.data.mapping.PropertyReferenceException: No property findAll found for type User - Redis 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151759/

相关文章:

Java 访问桥 - 使用 getAccessibleContextAt 时出现问题

java - 这是如何编写此 <class name> <object name > = new constructor name 的正确方法?

java - java中如何动态计算公式?

java - 如何使用 Apache POI 浏览工作簿的工作表?

java - 如何为基于当前用户查找类的 CRUD 存储库方法编写自定义查询?

regex - 方括号和星号作为 Redis 键中的 WildCards 字符或匹配扫描

.net - redis 获取列表,其中有键开头

angularjs - 如何检索oracle sql查询的进度?

java - 在 spring boot schema.sql 文件中执行过程的问题

java - Bit Set 真的比 Sorted Set 操作更快吗?