10
回答
基于hibernate实现的jpa接口,multiselect不起作用
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

之前问过一次,没有人回答。。。。,再来问多一次~

使用jpa的仓库接口来查询数据,本意是根据条件来查询一个实体的指定属性字段,但是现在却把这个实体的所有属性都查出来了。问问大神们怎么解决。采用的是实现JpaSpecificationExecutor接口:代码如下:

-------------

Specification<Users> spec = new Specification<Users>() {
    @Override
    public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query,CriteriaBuilder cb) {
        // TODO Auto-generated method stub
        List<Predicate> ps = new ArrayList<Predicate>();
        ps.add(cb.equal(root.<String>get("userName"), userName));
          
        List<Selection<?>> selections = new ArrayList<Selection<?>>();
        selections.add(root.get("id").alias("id"));
        selections.add(root.get("birth").alias("birth"));
        selections.add(root.get("userName").alias("userName"));
          
          
        query.multiselect(selections);
          
        query.where(ps.toArray(new Predicate[ps.size()]));
        return query.getGroupRestriction();
    }
};
    return repository.findOne(spec);



预期是查到id,birth,userName的,现在却是这样:

Hibernate: 
    select
        users0_.id as id1_22_,
        users0_.create_time as create_t2_22_,
        users0_.update_time as update_t3_22_,
        users0_.version as version4_22_,
        users0_.birth as birth5_22_,
        users0_.gendar as gendar6_22_,
        users0_.password as password7_22_,
        users0_.status as status8_22_,
        users0_.user_name as user_nam9_22_ 
    from
        users users0_ 
    where
        users0_.user_name=?



我已经multiselect里设置了,为什么还是这样。。。望解答  

------------------

在网上找了很多都说是在multiselect里用root设置自己要查询的字段属性,可是根本没效果啊~~~

举报
顶部