lundi 13 mai 2019

Reflection invoke specification method Spring Data JPA

I must invoke method findOne with Spec JPA repository by entityClass and get exception.

I tried to explicitly specify the class of the repository, but it does not contain the required method


Repositories repositories = new Repositories(applicationContext);
        CrudRepository entityRepository = (CrudRepository) repositories.getRepositoryFor(entityClass);
Advised repoProxy = (Advised)repositories.getRepositoryFor(entityClass);
        Class<?>[] interfaces = repoProxy.getProxiedInterfaces();

List<Method> methods = Arrays.stream(interfaces)
                .flatMap(c -> Arrays.stream(ReflectionUtils.getAllDeclaredMethods(c))).filter(method->{
            if (method.getParameterCount()>0) {
                Class<?> clazz = method.getParameterTypes()[0];
                return (clazz.equals(Specification.class) && method.getName().equals("findOne"));
            }
            return false;
        }).collect(toList());


Specifications spec = //eny spec;



       try {
            methods.get(0).invoke(JpaSpecificationExecutor.class.newInstance(), spec);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


Caused by: java.lang.NoSuchMethodException: org.springframework.data.jpa.repository.JpaSpecificationExecutor.()





Aucun commentaire:

Enregistrer un commentaire