Spring InvalidDataAccessApiUsageException:参数值元素与预期类型不匹配

时间:2019-01-16 12:25:40

标签: spring specifications

我已经回答了这个问题,但是没有任何反馈。因此,我将尝试提出不同的问题。 我想将规格用于搜索请求。但似乎该规范无法访问或某些东西,因为它告诉我:

javax.el.ELException: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [com.auth0.samples.bootfaces.TelefonbuchSpecifications$1@20079547] did not match expected type [java.lang.String (n/a)];

此:[com.auth0.samples.bootfaces.TelefonbuchSpecifications$1@20079547] @ ...似乎不正确。

我只是尝试遵循此官方Spring教程:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/或DZone的教程是相同的https://dzone.com/articles/using-spring-data-jpa-specification

我的课程:

public interface TelefonbuchRepository extends JpaRepository<Telefonbuch, Long>, JpaSpecificationExecutor<Telefonbuch> {

    public List<Telefonbuch> findByVornameOrNachname(String vorname, String nachname);
    public List<Telefonbuch> findByVorname(Specification<Telefonbuch> spec);
}

Telefonbuch规格:

public class TelefonbuchSpecifications implements Specification<Telefonbuch> { //implements was a try

    public static Specification<Telefonbuch> hasVorname(String vorname) {
        return (root, query, cb) -> {
            //return cb.equal(root.get(Telefonbuch_.vorname), "%"+vorname.toLowerCase()+"%");
            Predicate equalPredicate = cb.like(cb.lower(root.get(Telefonbuch_.vorname)), "%"+vorname.toLowerCase()+"%");
            return equalPredicate;
        };
    }

    @Override
    public Predicate toPredicate(Root<Telefonbuch> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        // TODO Auto-generated method stub
        return null;
    }

SearchController中的方法:

 public void search(String vorname, String nachname, String telefonnummer, String handynummer) {  
List<Telefonbuch> list = telefonbuchRepository.findByVorname(TelefonbuchSpecifications.hasVorname(vorname));

vorname是我的Telefonbuch模型中定义的字符串。如果您需要更多课程或信息,请告诉我。我很沮丧这也是该项目的图片: enter image description here

1 个答案:

答案 0 :(得分:0)

感谢@ M.Deinum

解决方案要大一些。

enter image description here

删除类SpecificationTelefonbuchSpecifications

只需将TelefonbuchSpecifications作为辅助类插入TelefonbuchRepository

public interface TelefonbuchRepository extends JpaRepository<Telefonbuch, Long>, JpaSpecificationExecutor<Telefonbuch> {

    public class TelefonbuchSpecifications {

        public static Specification<Telefonbuch> hasVorname(String vorname) {
            return (root, query, cb) -> {
                return cb.like(cb.lower(root.get(Telefonbuch_.vorname)), "%" + vorname.toLowerCase() + "%");
            };
        }

        public static Specification<Telefonbuch> hasNachname(String nachname) {
            return (root, query, cb) -> {
                return cb.like(cb.lower(root.get(Telefonbuch_.nachname)), "%" + nachname.toLowerCase() + "%");
            };
        }

    }
}

对于search中的方法SucheController,然后:

List<Telefonbuch> list = telefonbuchRepository.findAll(TelefonbuchSpecifications.hasVorname(vorname));