List of usage examples for javax.persistence.criteria CriteriaQuery getRoots
Set<Root<?>> getRoots();
From source file:com.zero.dao.impl.BaseDaoImpl.java
protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters, List<Order> orders) { Assert.notNull(criteriaQuery);//from w ww .j ava 2 s . c o m Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); addRestrictions(criteriaQuery, filters); TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (first != null) { query.setFirstResult(first); } if (count != null) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.groupbuy.dao.impl.BaseDaoImpl.java
protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) { Assert.notNull(criteriaQuery);/*from w w w .j a v a 2s. c om*/ Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); if (pageable == null) { pageable = new Pageable(); } CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Root<T> root = getRoot(criteriaQuery); addRestrictions(criteriaQuery, pageable); addOrders(criteriaQuery, pageable); if (criteriaQuery.getOrderList().isEmpty()) { if (OrderEntity.class.isAssignableFrom(entityClass)) { criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME))); } else { criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME))); } } long total = count(criteriaQuery, null); int totalPages = (int) Math.ceil((double) total / (double) pageable.getPageSize()); if (totalPages < pageable.getPageNumber()) { pageable.setPageNumber(totalPages); } TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); query.setMaxResults(pageable.getPageSize()); return new Page<T>(query.getResultList(), total, pageable); }
From source file:net.groupbuy.dao.impl.BaseDaoImpl.java
protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { Assert.notNull(criteriaQuery);/*from w w w .j a va2s .co m*/ Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); addRestrictions(criteriaQuery, filters); CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class); for (Root<?> root : criteriaQuery.getRoots()) { Root<?> dest = countCriteriaQuery.from(root.getJavaType()); dest.alias(getAlias(root)); copyJoins(root, dest); } Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType()); countCriteriaQuery.select(criteriaBuilder.count(countRoot)); if (criteriaQuery.getGroupList() != null) { countCriteriaQuery.groupBy(criteriaQuery.getGroupList()); } if (criteriaQuery.getGroupRestriction() != null) { countCriteriaQuery.having(criteriaQuery.getGroupRestriction()); } if (criteriaQuery.getRestriction() != null) { countCriteriaQuery.where(criteriaQuery.getRestriction()); } return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:org.jdal.dao.jpa.JpaDao.java
/** * Create a TypedQuery from a request page * @param page request page/*from ww w . j av a 2s . co m*/ * @return new TypedQuery */ @SuppressWarnings("unchecked") private <K> TypedQuery<K> getCriteriaQuery(Page<K> page) { CriteriaQuery<K> criteria = getCriteria(page); CriteriaQuery<Long> countCriteria = (CriteriaQuery<Long>) getCriteria(page); CriteriaBuilder cb = em.getCriteriaBuilder(); Root<?> root = countCriteria.getRoots().iterator().next(); countCriteria.select(cb.count(root)); page.setCount((em.createQuery(countCriteria).getSingleResult()).intValue()); criteria.orderBy(getOrder(page, criteria)); // Add default select to entity class if none was set. if (criteria.getSelection() == null) { criteria.select((Selection<? extends K>) root); } return em.createQuery(criteria); }
From source file:net.groupbuy.dao.impl.BaseDaoImpl.java
protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters, List<Order> orders) { Assert.notNull(criteriaQuery);/*from w w w . j a v a2s . c o m*/ Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Root<T> root = getRoot(criteriaQuery); addRestrictions(criteriaQuery, filters); addOrders(criteriaQuery, orders); if (criteriaQuery.getOrderList().isEmpty()) { if (OrderEntity.class.isAssignableFrom(entityClass)) { criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME))); } else { criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME))); } } TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (first != null) { query.setFirstResult(first); } if (count != null) { query.setMaxResults(count); } return query.getResultList(); }
From source file:com.impetus.kundera.persistence.CriteriaQueryTranslator.java
/** * Method to translate criteriaQuery into JPQL. * //from w w w.jav a2 s .c o m * @param criteriaQuery * criteria query. * * @return JPQL string. */ static <S> String translate(CriteriaQuery criteriaQuery) { QueryBuilder builder = new CriteriaQueryTranslator.QueryBuilder(); // validate if criteria query is valid /** * select, from clause is mandatory * * multiple from clause not support where clause is optional * */ Selection<S> select = criteriaQuery.getSelection(); if (select != null) { builder.appendSelectClause(); } if (select.getClass().isAssignableFrom(DefaultCompoundSelection.class) && ((CompoundSelection) select).isCompoundSelection()) { List<Selection<?>> selections = ((CompoundSelection) select).getCompoundSelectionItems(); builder.appendMultiSelect(selections); } else if (select instanceof AggregateExpression) { builder.appendAggregate(((AggregateExpression) select).getAggregation()); } else { String alias = select.getAlias(); if (!StringUtils.isEmpty(alias)) { builder.appendAlias(alias); } Attribute attribute = ((DefaultPath) select).getAttribute(); if (attribute != null) { builder.appendAttribute(attribute); } } Class<? extends S> clazzType = select.getJavaType(); Set<Root<?>> roots = criteriaQuery.getRoots(); Root<?> from = roots.iterator().next(); Class entityClazz = from.getJavaType(); builder.appendFromClause(); // select.alias(paramString) builder.appendFrom(entityClazz); builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias()); Predicate where = criteriaQuery.getRestriction(); // this could be null. if (where != null) { builder.appendWhereClause(); List<Expression<Boolean>> expressions = where.getExpressions(); for (Expression expr : expressions) { builder.appendWhere(expr, from.getAlias()); } } List<Order> orderings = criteriaQuery.getOrderList(); if (orderings != null) { if (!orderings.isEmpty()) { builder.appendOrderClause(where == null); } for (Order order : orderings) { builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias()); builder.appendOrdering(order); } } return builder.getQuery(); // check that roots has to be one. multiple clause not yet supported }
From source file:org.agric.oxm.utils.JpaUtils.java
/** * Find the Root with type class on CriteriaQuery Root Set * /*from w w w .j a v a 2 s .co m*/ * @param <T> * root type * @param query * criteria query * @param clazz * root type * @return Root<T> of null if none */ public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) { for (Root<?> r : query.getRoots()) { if (clazz.equals(r.getJavaType())) { return (Root<T>) r.as(clazz); } } return null; }
From source file:org.agric.oxm.utils.JpaUtils.java
/** * Copy Criteria without Selection//from w ww . ja va 2 s . c om * * @param from * source Criteria * @param to * destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); to.having(from.getGroupRestriction()); to.where(from.getRestriction()); to.orderBy(from.getOrderList()); }
From source file:org.apache.ambari.server.api.query.JpaSortBuilder.java
/** * Builds the list of sort orders based on the supplied request and JPA * predicate visitor.//from w w w. j a va2 s . c om * * @param sortRequests * the Ambari sort request properties to turn into a JPA sort * request. If {@code null} or the {@link SortRequestProperty} list * is null, an empty list is returned. * @param visitor * a visitor that knows how to convert the Ambari properties into * {@link SingularAttribute} (not {@code null}). * @return a list of sorts or an empty list if none (never {@code null}). */ public List<Order> buildSortOrders(SortRequest sortRequest, JpaPredicateVisitor<T> visitor) { if (null == sortRequest || null == sortRequest.getProperties()) { return Collections.emptyList(); } CriteriaBuilder builder = visitor.getCriteriaBuilder(); List<SortRequestProperty> sortProperties = sortRequest.getProperties(); List<Order> sortOrders = new ArrayList<Order>(sortProperties.size()); for (SortRequestProperty sort : sortProperties) { String propertyId = sort.getPropertyId(); List<? extends SingularAttribute<?, ?>> singularAttributes = visitor.getPredicateMapping(propertyId); if (null == singularAttributes || singularAttributes.size() == 0) { continue; } Path<?> path = null; for (SingularAttribute<?, ?> singularAttribute : singularAttributes) { if (null == path) { CriteriaQuery<T> query = visitor.getCriteriaQuery(); Set<Root<?>> roots = query.getRoots(); // if there are existing roots; use the existing roots to prevent more // roots from being added potentially causing a cartesian product // where we don't want one if (null != roots && !roots.isEmpty()) { Iterator<Root<?>> iterator = roots.iterator(); while (iterator.hasNext()) { Root<?> root = iterator.next(); Class<?> visitorEntityClass = visitor.getEntityClass(); if (ObjectUtils.equals(visitorEntityClass, root.getJavaType()) || ObjectUtils.equals(visitorEntityClass, root.getModel().getJavaType())) { path = root.get(singularAttribute.getName()); break; } } } // no roots exist already which match this entity class, create a new // path if (null == path) { path = query.from(visitor.getEntityClass()).get(singularAttribute.getName()); } } else { path = path.get(singularAttribute.getName()); } } Order sortOrder = null; if (sort.getOrder() == org.apache.ambari.server.controller.spi.SortRequest.Order.ASC) { sortOrder = builder.asc(path); } else { sortOrder = builder.desc(path); } sortOrders.add(sortOrder); } return sortOrders; }
From source file:org.jboss.pressgang.ccms.filter.utils.JPAUtils.java
/** * Copy Criteria without Selection/*from ww w .java 2 s . c om*/ * * @param from source Criteria * @param to destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } if (from.getGroupList() != null) to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); if (from.getRestriction() != null) to.where(from.getRestriction()); if (from.getOrderList() != null) to.orderBy(from.getOrderList()); }