當前位置: 首頁>>代碼示例>>Java>>正文


Java CriteriaQuery.groupBy方法代碼示例

本文整理匯總了Java中javax.persistence.criteria.CriteriaQuery.groupBy方法的典型用法代碼示例。如果您正苦於以下問題:Java CriteriaQuery.groupBy方法的具體用法?Java CriteriaQuery.groupBy怎麽用?Java CriteriaQuery.groupBy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.persistence.criteria.CriteriaQuery的用法示例。


在下文中一共展示了CriteriaQuery.groupBy方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: countWhenGroupBy

import javax.persistence.criteria.CriteriaQuery; //導入方法依賴的package包/類
private long countWhenGroupBy(final Specification<T> specification, final DatatableQuery<?, T> input) {
  	final EntityManager entityManager = entityManagerFactory.createEntityManager();
      final CriteriaBuilder qb = entityManager.getCriteriaBuilder();
      final CriteriaQuery<Long> cq = qb.createQuery(Long.class);
      final Root<T> from = cq.from(this.entity);
      
      cq.select(qb.literal(1L));
      if (specification != null) {
          cq.where(specification.toPredicate(from, cq, qb));
      }
      if (input.getGroupByColumns() != null && !input.getGroupByColumns().isEmpty()) {
          final List<Expression<?>> groupByList = new ArrayList<Expression<?>>();
          DatatableHelper.getExpressions(from, input.getGroupByColumns(), groupByList);
          cq.groupBy(groupByList);
      }
      final long ret = entityManager.createQuery(cq).getResultList().size();
      entityManager.close();
return ret;
  }
 
開發者ID:andresoviedo,項目名稱:datatable-java,代碼行數:20,代碼來源:DatatableDAOImpl.java

示例2: getCountQuery

import javax.persistence.criteria.CriteriaQuery; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> TypedQuery<Long> getCountQuery(CriteriaQuery<T> cq) {
	Class<T> domainClass = cq.getResultType();
	EntityManager em = getEntityManager(domainClass);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> countCq = cb.createQuery(Long.class);
	Root<T> root;
	if (cq.getRestriction() != null) {
		countCq.where(cq.getRestriction());
	}
	if (cq.getGroupRestriction() != null) {
		countCq.having(cq.getGroupRestriction());
	}
	if (cq.getRoots().isEmpty()) {
		root = countCq.from(domainClass);
	} else {
		countCq.getRoots().addAll(cq.getRoots());
		root = (Root<T>) countCq.getRoots().iterator().next();
	}
	countCq.groupBy(cq.getGroupList());
	if (cq.isDistinct()) {
		countCq.select(cb.countDistinct(root));
	} else {
		countCq.select(cb.count(root));
	}

	return em.createQuery(countCq);
}
 
開發者ID:muxiangqiu,項目名稱:linq,代碼行數:29,代碼來源:JpaUtil.java

示例3: findAllImpl1

import javax.persistence.criteria.CriteriaQuery; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
  private <R> List<R> findAllImpl1(final DatatableQuery<R, T> input, final Specifications<T> specifications,
      final Pageable pageable) {

      // create query
  	final EntityManager entityManager = entityManagerFactory.createEntityManager();
      final CriteriaBuilder qb = entityManager.getCriteriaBuilder();
      final CriteriaQuery<T> cq = qb.createQuery(this.entity);

      // Etablir entité racine
      final Root<T> from = cq.from(this.entity);

      // select
      cq.select(from);

      // where
      if (specifications != null) {
          cq.where(specifications.toPredicate(from, cq, qb));
      }

      // group by
      if (input.getGroupByColumns() != null && !input.getGroupByColumns().isEmpty()) {
          final List<Expression<?>> groupByList = new ArrayList<Expression<?>>();
          DatatableHelper.getExpressions(from, input.getGroupByColumns(), groupByList);
          cq.groupBy(groupByList);
      }

      // order by
      if (pageable != null && pageable.getSort() != null) {
          cq.orderBy(DatatableHelper.getOrderBy(from, qb, pageable));
      }

      // page limit
      final TypedQuery<T> q = entityManager.createQuery(cq);
      if (pageable != null) {
          q.setFirstResult(pageable.getOffset());
          q.setMaxResults(pageable.getPageSize());
      }

      // execute query
      final List<R> ret = (List<R>) q.getResultList();
      entityManager.close();
return ret;
  }
 
開發者ID:andresoviedo,項目名稱:datatable-java,代碼行數:45,代碼來源:DatatableDAOImpl.java

示例4: findAllImpl2

import javax.persistence.criteria.CriteriaQuery; //導入方法依賴的package包/類
private <R> List<R> findAllImpl2(final DatatableQuery<R, T> input, final Specifications<T> specifications,
      final Pageable pageable) {
      
      // create query
  	final EntityManager entityManager = entityManagerFactory.createEntityManager();
      final CriteriaBuilder qb = entityManager.getCriteriaBuilder();        
      final CriteriaQuery<R> cq = qb.createQuery(input.getQueryClass());

      // Etablir entité racine
      final Root<T> from = cq.from(this.entity);
      
      // select for custom class
      final List<Selection<?>> selectList = new ArrayList<Selection<?>>();
      if (input.getGroupByColumns() != null) {
          DatatableHelper.getExpressions(from, input.getGroupByColumns(), selectList);
      } else {
          DatatableHelper.getExpressions(from, input.getColumns(), selectList);
      }
      cq.multiselect(selectList);

      // where
      if (specifications != null) {
          cq.where(specifications.toPredicate(from, cq, qb));
      }

      // group by
      if (input.getGroupByColumns() != null && !input.getGroupByColumns().isEmpty()) {
          final List<Expression<?>> groupByList = new ArrayList<Expression<?>>();
          DatatableHelper.getExpressions(from, input.getGroupByColumns(), groupByList);
          cq.groupBy(groupByList);
      }

      // order by
      if (pageable != null && pageable.getSort() != null) {
          cq.orderBy(DatatableHelper.getOrderBy(from, qb, pageable));
      }

      // page limit
      final TypedQuery<R> q = entityManager.createQuery(cq);
      if (pageable != null) {
      	LOGGER.log(Level.FINE, "Pageable: offset: {0}, page size: {1}", new Object[]{pageable.getOffset(), pageable.getPageSize()});
          q.setFirstResult(pageable.getOffset());
          q.setMaxResults(pageable.getPageSize());
      }

      // execute query
      final List<R> ret = q.getResultList();
      entityManager.close();
return ret;
  }
 
開發者ID:andresoviedo,項目名稱:datatable-java,代碼行數:51,代碼來源:DatatableDAOImpl.java


注:本文中的javax.persistence.criteria.CriteriaQuery.groupBy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。