List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery
CriteriaQuery<Object> createQuery();
CriteriaQuery
object. From source file:com.sapito.db.dao.AbstractDao.java
public List<T> findBySpecificField(String field, Object fieldContent, String predicates, LinkedHashMap<String, String> ordering, LinkedList<String> grouping) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); Predicate predicate = null;/*w ww . j ava 2s . com*/ if (predicates.equals("equal")) { predicate = cb.equal(root.get(field), fieldContent); } else if (predicates.equals("likelower")) { predicate = cb.like(cb.lower(root.<String>get(field)), fieldContent.toString()); } else if (predicates.equals("like")) { predicate = cb.like(root.<String>get(field), "%" + fieldContent.toString() + "%"); } cq.select(root); cq.where(predicate); if (ordering != null) { Set<String> set = ordering.keySet(); List<Order> orders = new ArrayList<>(); for (String orderingField : set) { Order order; if (ordering.get(orderingField).equals("ASC")) { order = cb.asc(root.get(orderingField)); } else { order = cb.desc(root.get(orderingField)); } orders.add(order); } cq.orderBy(orders); } if (grouping != null) { Iterator iterator = grouping.iterator(); List<Expression> groups = new LinkedList<>(); while (iterator.hasNext()) { groups.add(root.get(iterator.next().toString())); } cq.groupBy(groups); } Query query = entityManager.createQuery(cq); query.setMaxResults(MAX_RECORDS_RETURNED); return query.getResultList(); }
From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java
@Override public List<PurchaseOrder> findAll() { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); javax.persistence.criteria.CriteriaQuery cq = cb.createQuery(); Root<PurchaseOrder> root = cq.from(PurchaseOrder.class); cq.select(root);//from w ww . ja v a2s .co m ParameterExpression<String> status = cb.parameter(String.class); // cq.where(cb.notEqual(invRoot.get("status"), status)); cq.where(cb.notEqual(root.get("deleted"), 'Y')); cq.orderBy(cb.desc(root.get("purchaseOrderId"))); Query query = getEntityManager().createQuery(cq); // query.setParameter(status, PaymentStatusEnum.DELETE.getValue()); List<PurchaseOrder> list = query.getResultList(); for (PurchaseOrder wo : list) { List<PurchaseOrderItems> tempDel = new ArrayList<>(); for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) { if (wot.isDeleted()) { tempDel.add(wot); } } wo.getPurchaseOrderItems().removeAll(tempDel); } return list; }
From source file:utils.jpa.EntityResource.java
public int getAllCount(TableSearchQuery tb, List<String> fieldsSearchBy) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); cq.select(cb.count(root));/*from ww w . j a v a 2 s .co m*/ Query query = tb.createQuery(em, cb, cq, root); if (query == null) { query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy); } try { return ((Long) query.getSingleResult()).intValue(); } catch (NoResultException ex) { return 0; } }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * count all entity//from w w w.j ava 2s . c om * * @return */ public int count() { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> rt = cq.from(entityClass); cq.select(cb.count(rt)); Query q = em.createQuery(cq); Object result = q.getSingleResult(); return result == null ? 0 : ((Long) result).intValue(); }
From source file:org.jdal.dao.jpa.JpaDao.java
/** * Build CriteriaQuery using declared JpaCriteriaBuilder in filterMap * @param page/*w w w . ja va2 s.co m*/ * @return a CriteriaQuery from filter */ @SuppressWarnings("unchecked") private <K> CriteriaQuery<K> getCriteria(Page<K> page) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<K> c = (CriteriaQuery<K>) cb.createQuery(); Filter filter = null; if (page.getFilter() instanceof Filter) { filter = (Filter) page.getFilter(); JpaCriteriaBuilder<K> jcb = (JpaCriteriaBuilder<K>) criteriaBuilderMap.get(filter.getFilterName()); if (jcb != null) { if (log.isDebugEnabled()) log.debug("Found JpaCriteriaBuilder for filter: " + filter.getFilterName()); // build criteria c = jcb.build(c, cb, filter); } else { log.error("No CriteriaBuilder found for filter name [" + filter.getFilterName() + "]"); c.from(entityClass); } } else { c.select((Selection<? extends K>) c.from(getEntityClass())); } return c; }
From source file:com.hiperium.dao.common.generic.GenericDAO.java
/** * /* ww w .ja va 2 s . c o m*/ * @param bypassCache * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected List<T> findAll(boolean bypassCache) { CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(); criteriaQuery.select(criteriaQuery.from(this.entityClass)); Query query = this.entityManager.createQuery(criteriaQuery); if (bypassCache) { this.setBypassCahe(query); } return query.getResultList(); }