JPA count(*) 使用 CriteriaBuilder

JPA count(*) using CriteriaBuilder

我对 CriteriaBuilder API 有疑问。我是 JPA 的新手,我想创建一个 count(*) 以为 CriteriaBuilder。我需要创建的计数需要使用我在这个动态查询中使用的相同参数,我创建

我有创建动态查询的服务,但我不知道如何使用相同的参数创建 count(*)

package com.sanmina.rohsappapirest.model.services.imp;


/**
 * @author edgar_conrado on 07/07/2020
 *
 */
@Service
public class ItemFilterServiceImp implements IItemFilterService {

    @Autowired
    private IItemFilterDao itemFilterDao;

    
    /**
     * This function to get the items providing parameters
     * 
     * @param manufacturers     Array with the manufacturer to found
     * @param mpnReachs         Array with the mpnReachs to found
     *  
     * @return List with the items
     */
    @Override
    @Transactional(readOnly = true)
    public List<ItemFilter> findByInManufacturer(String[] manufacturers, String[] mpnReachs) {      
        return itemFilterDao.findAll(new Specification<ItemFilter>() {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<ItemFilter> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {          
                List<Predicate> predicates = new ArrayList<>();
                
                // Criteria for Manufacturer
                if (manufacturers!=null) {                  
                    // Convert the array in a List
                    List<String> manufacturerList = new ArrayList<String>();
                    for (String l : manufacturers) {
                        manufacturerList.add(l);
                    }

                    //Create the query
                    In<String> predicate = criteriaBuilder.in(root.get("manufacturer"));                                        
                    manufacturerList.forEach(t -> predicate.value(t));  

                    predicates.add(predicate);
                    //predicates.add(criteriaBuilder.and(criteriaBuilder.in(root.get("manufacturer"))));                    
                }
                
                // Criteria for Reach
                if (mpnReachs!=null) {                  
                    // Convert the array in a List
                    List<String> reachList = new ArrayList<String>();
                    for (String l : mpnReachs) {
                        reachList.add(l);
                    }

                    //Create the query
                    In<String> predicate = criteriaBuilder.in(root.get("mpnReach"));                                        
                    reachList.forEach(t -> predicate.value(t));                 
                    predicates.add(predicate);                  
                }
                                        
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }

        });
    }
        
}

有什么建议吗?

使用此代码。

CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = qb.createQuery(Long.class);
cq.select(qb.count(cq.from(MyEntity.class)));
cq.where(/*your stuff*/);
return entityManager.createQuery(cq).getSingleResult();