Reputation: 53
I use spring boot, query from database like this:
@Repository
public interface MyRepository extends CrudRepository<MyEntity, Integer> {
List<MyEntity> findAllByIdIn(List<Integer> ids);
}
And I want to invoke this method "findAllByIdIn" dynamically:
Object bean = context.getBean("myRepository");
bean.getClass().getMethod("findAllByIdIn").invoke(bean, ids);
I got an exception:
java.lang.NoSuchMethodException: com.sun.proxy.$Proxy145.findAllByIdIn()
How can I invoke that method?
Upvotes: 2
Views: 5054
Reputation: 15878
From the error java.lang.NoSuchMethodException: com.sun.proxy.$Proxy145.findAllByIdIn()
it is clear that your code is trying to get a method with no argument which is really not present.
So try to pass parameters also as it is expecting parameters something like below.
getMethod("findAllByIdIn",List.class)
Upvotes: 2
Reputation: 8297
getMethod accepts 2 parameters. The first one is a method name and the second one is vararg of method parameters types.
Your code tries to get findAllByIdIn
method without arguments, but your repository does not have it.
So the fix is:
bean.getClass().getMethod("findAllByIdIn", List.class)
Upvotes: 3