How to return custom result object with JPA specification dynamic query?

Viewed 135

I want to return CustomResultObject which is not ENTITY but a statistics pojo, how to use it with JPA specification dynamic query? The sample code looks like:

Specification<CustomResultObject> spec = new Specification<CustomResultObject>() {  
    public Predicate toPredicate(Root<UserModel> root,  
            CriteriaQuery<?> query, CriteriaBuilder cb) {  
        Predicate p1 = cb.like(root.get("name").as(String.class), "%"+um.getName()+"%");  
        Predicate p2 = cb.equal(root.get("uuid").as(Integer.class), um.getUuid());  
        Predicate p3 = cb.gt(root.get("age").as(Integer.class), um.getAge());  
        query.where(cb.and(p3,cb.or(p1,p2)));  
        query.orderBy(cb.desc(root.get("uuid").as(Integer.class)));  
          
        return query.getRestriction();  
    }  
};
0 Answers
Related