List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery
<T> CriteriaQuery<T> createQuery(Class<T> resultClass);
CriteriaQuery
object with the specified result type. From source file:eu.uqasar.service.AbstractService.java
/** * Method to get the entity of class by a given name. * @param clazz Class/*from w w w .ja v a 2 s.c om*/ * @param name attribute name of the searched entity * @return T entity */ public <T extends Persistable> T getByName(Class<T> clazz, String name) { logger.infof("loading %s with name %d ...", getReadableClassName(clazz), name); T entity = null; CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz); Root<T> ent = criteriaQuery.from(clazz); criteriaQuery.select(ent).where(criteriaBuilder.equal(ent.get("name"), name)); List<T> entities = em.createQuery(criteriaQuery).getResultList(); if (entities != null && entities.size() > 0) { entity = entities.get(0); } return entity; }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal createOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }//from w w w.j a v a 2 s .co m if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal completeOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate)); }// w w w.ja v a 2 s . c o m if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:com.sfs.ucm.controller.HelpContentAction.java
/** * load resources//from w ww . j a v a2 s . c o m * * @throws UCMException */ private void loadList() throws UCMException { try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Help> c = cb.createQuery(Help.class); Root<Help> obj = c.from(Help.class); c.select(obj).orderBy(cb.asc(obj.get("keyword"))); this.helpItems = em.createQuery(c).getResultList(); this.itemCount = this.helpItems.size(); } catch (Exception e) { throw new UCMException(e); } }
From source file:com.zero.dao.impl.BaseDaoImpl.java
public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return findList(criteriaQuery, first, count, filters, orders); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
public Page<T> findPage(Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return findPage(criteriaQuery, pageable); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
public long count(Filter... filters) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return count(criteriaQuery, filters != null ? Arrays.asList(filters) : null); }
From source file:com.sfs.captor.controller.RequirementAction.java
/** * Get count of requirement rules//from w w w . ja v a 2 s . c om * * @return count */ private Long getRequirementRuleCount() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> c = cb.createQuery(Long.class); c.select(cb.count(c.from(RequirementRule.class))); return em.createQuery(c).getSingleResult(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * use customize object in select//from ww w. ja va 2 s . c om */ public void doSelect3() { CriteriaBuilder cb = em.getCriteriaBuilder(); // return type is a customized object CriteriaQuery<EmployeeBasicInfo> c = cb.createQuery(EmployeeBasicInfo.class); Root<Employee> e = c.from(Employee.class); // method 1 : must use multiple select, cannot use select method, because of strong type check c.multiselect(e.get("name")); showResult(c); // method 2 : use select and cb.construct c.select(cb.construct(EmployeeBasicInfo.class, e.get("name"))); showResult(c); // method 3 : use multiple select and cb.construct // NOTE : UNSUPPORT! // CriteriaQuery<EmployeeBasicInfo> c2 = cb.createQuery(EmployeeBasicInfo.class); // Root<Employee> e2 = c2.from(Employee.class); // c2.multiselect(cb.construct(EmployeeBasicInfo.class, e2.get("name"))); // showResult(c2); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * list entity by CriteriaInfo/* ww w. j a v a 2 s . co m*/ * * @param criteriaInfo * @return PagedResult */ public PagedResult<T> list(CriteriaInfo criteriaInfo) { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityClass); Root<T> userRoot = cq.from(entityClass); cq.select(userRoot); ParamInfo paramInfo = criteriaInfo.getParamInfo(); PageInfo pageInfo = criteriaInfo.getPageInfo(); SortInfo sortInfo = criteriaInfo.getSortInfo(); //build query for paramInfo if (paramInfo != null) { Set<Predicate> andCriteria = new HashSet(); Set<Predicate> orCriteria = new HashSet(); for (ParamItem item : paramInfo.getParamItems()) { Predicate predicate; if (item.getValue() instanceof String) { //fuzy search for string String regExp = "%" + item.getValue() + "%"; predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp); } else { predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue()); } switch (item.getOperator()) { case AND: andCriteria.add(predicate); break; case OR: orCriteria.add(predicate); break; } } if (orCriteria.size() > 0) { Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()])); andCriteria.add(or); } if (andCriteria.size() > 0) { Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()])); cq.where(and); } } //build query for sortInfo Set<Order> orderPredicate = new HashSet<>(); if (sortInfo != null) { for (SortItem item : sortInfo.getSortItems()) { if (item.isDescending()) { orderPredicate.add(cb.desc(userRoot.get(item.getFieldName()))); } else { orderPredicate.add(cb.asc(userRoot.get(item.getFieldName()))); } } } if (orderPredicate.size() > 0) { cq.orderBy(orderPredicate.toArray(new Order[orderPredicate.size()])); } TypedQuery<T> query = em.createQuery(cq); //set result range if (pageInfo != null) { query.setFirstResult(pageInfo.getOffset()); query.setMaxResults(pageInfo.getSize()); } int totalSize; if (paramInfo != null && paramInfo.getParamItems().size() > 0) { totalSize = count(paramInfo); } else { totalSize = count(); } return new PagedResult(query.getResultList(), totalSize); }