简体   繁体   English

NoSuchMethodException 使用反射与 Spring JPARespository 方法(例如:findByName)

[英]NoSuchMethodException when using reflection with Spring JPARespository Methods (eg: findByName)

I'm trying implement multiple filters with JPA Query Methods offered by Spring Data JPA .我正在尝试使用Spring Data JPA提供的JPA 查询方法实现多个过滤器。

Dynamic filter eg: findByNameContainingCreatedAtBefore动态过滤器 例如: findByNameContainingCreatedAtBefore

(eg: findByName ) and I used Java Reflection to get a method from JPA Repository and invoke dynamically. (例如: findByName ),我使用 Java 反射从JPA 存储库获取方法并动态调用。

Method method = institutionRepository.getClass().getMethod("findByName", String.class);

But the above line throws NoSuchMethodException .但是上面的行抛出了NoSuchMethodException

Are there any ways to fix the above issue?有没有办法解决上述问题?

I bet there is no findByName in your source code and you're expecting to see the method generated by the spring-data-jpa.我敢打赌,您的源代码中没有findByName ,您希望看到 spring-data-jpa 生成的方法。 This will not happen, because the method isn't added by spring to the repository itself, but to the proxy over that repository.这不会发生,因为该方法不是由 spring 添加到存储库本身,而是添加到该存储库上的代理。 That's why it has to be a bean - so that it can be wrapped by spring with a proxy to provide all the functionality.这就是为什么它必须是一个 bean - 以便它可以由 spring 包装,并带有代理以提供所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM