Guest User

Untitled

a guest
Oct 21st, 2019
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public Page buscaContendoCategoria(Pageable pageable, List categorias){
  2. final List orPredicates = new ArrayList();
  3. CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
  4. CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Noticia.class);
  5. Root noticia = criteriaQuery.from(Noticia.class);
  6. List predicates = new ArrayList<>();
  7.  
  8. for(Categoria categoria : categorias){
  9. orPredicates.add(criteriaBuilder.isMember(categoria , noticia.get("categorias")));
  10. }
  11. predicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
  12.  
  13. criteriaQuery.where(predicates.toArray(new Predicate[0]));
  14. criteriaQuery.orderBy(QueryUtils.toOrders(pageable.getSort(), noticia, criteriaBuilder));
  15.  
  16. TypedQuery query = entityManager.createQuery(criteriaQuery);
  17.  
  18. int totalRows = query.getResultList().size();
  19.  
  20. query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
  21. query.setMaxResults(pageable.getPageSize());
  22.  
  23. Page result = new PageImpl(query.getResultList(), pageable, totalRows);
  24.  
  25. return result;
  26. }
RAW Paste Data