JPA分页+条件查询的问题

service代码如下

//websiteservice
    public Page<Website> findPage(Integer page, Integer size, String name, String url) {
        Pageable pageable = (Pageable) new PageRequest(page, size);
        return websiteRepository.findAll(new Specification<Website>() {
            @Override
            public Predicate toPredicate(Root<Website> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namePath = root.get("name");
                Path<String> urlPath = root.get("url");
                /**
                 * 连接查询条件, 不定参数,可以连接0..N个查询条件
                 */
                query.where(cb.like(namePath, "%" + name + "%"), cb.like(urlPath, "%" + url + "%")); //这里可以设置任意条查询条件
                return null;
            }
        }, pageable);
    }
//economyservice
    
    public Page<Economy> findPage(Integer page, Integer size, String data_type) {
        Pageable pageable = (Pageable) new PageRequest(page, size);
        return economyRepository.findAll(new Specification<Economy>() {
            @Override
            public Predicate toPredicate(Root<Economy> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> dataTypePath = root.get("data_type");
                /**
                 * 连接查询条件, 不定参数,可以连接0..N个查询条件
                 */
                query.where(cb.like(dataTypePath, "%" + data_type + "%")); //这里可以设置任意条查询条件
                return null;
            }
        }, pageable);
    }
    

2个service代码是差不多的但是只有economy的生效了

Hibernate: select count(economy0_.id) as col_0_0_ from t_economy economy0_ where economy0_.data_type like ?
Hibernate: select economy0_.id as id1_0_, economy0_.area as area2_0_, economy0_.data_time as data_tim3_0_, economy0_.data_type as data_typ4_0_, economy0_.data_value as data_val5_0_ from t_economy economy0_ where economy0_.data_type like ? limit ? offset ?
Hibernate: select count(website0_.id) as col_0_0_ from t_website website0_ where (website0_.name like ?) and (website0_.url like ?)

从上面打印的sql来看应该是website少执行了一条sql,但是调用的方法都是一样的,为什么会出现个这个情况啊?

阅读 6.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题