Example usage for javax.persistence.criteria CriteriaQuery select

List of usage examples for javax.persistence.criteria CriteriaQuery select

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery select.

Prototype

CriteriaQuery<T> select(Selection<? extends T> selection);

Source Link

Document

Specify the item that is to be returned in the query result.

Usage

From source file:eu.domibus.common.dao.MessageLogDao.java

public List<MessageLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {

    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<MessageLogEntry> cq = cb.createQuery(MessageLogEntry.class);
    Root<MessageLogEntry> mle = cq.from(MessageLogEntry.class);
    cq.select(mle);
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "receivedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "receivedTo":
                        predicates.add(cb.lessThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(mle.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }/*from w w  w  .ja v  a 2  s.c  o m*/
                }
            } else {
                predicates.add(cb.equal(mle.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(mle.get(column)));
        } else {
            cq.orderBy(cb.desc(mle.get(column)));
        }

    }
    final TypedQuery<MessageLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:utils.jpa.EntityResource.java

private List<T> getAllPriv(@BeanParam TableSearchQuery tb, List<String> fieldsSearchBy) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    cq.select(root);

    //setting order
    if (tb.getOrderType().equals(OrderType.DESC)) {
        cq.orderBy(cb.desc(root.get(tb.getOrder())));
    } else {//from  www. j  a v  a 2  s .com
        cq.orderBy(cb.asc(root.get(tb.getOrder())));
    }

    Query query = tb.createQuery(em, cb, cq, root);
    if (query == null) {
        query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy);
    }

    if (tb.getOffset() > 0) {
        query.setFirstResult(tb.getOffset());
    }
    if (tb.getLimit() > 0) {
        query.setMaxResults(tb.getLimit());
    }

    return query.getResultList();
}

From source file:org.broadleafcommerce.inventory.admin.server.service.handler.InventorySkuCustomPersistenceHandler.java

@Override
public void applyAdditionalFetchCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto,
        PersistencePackage persistencePackage) {
    super.applyAdditionalFetchCriteria(filterMappings, cto, persistencePackage);
    // grab the fulfillment location off of the custom criteria from the
    // frontend//from w  ww .ja  v  a 2s . c om
    final Long locationId = Long.parseLong(persistencePackage.getCustomCriteria()[1]);
    FilterMapping f = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id"))
            .withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
                @Override
                public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder,
                        From root, String ceilingEntity, String fullPropertyName, Path explicitPath,
                        List directValues) {
                    // DetachedCriteria locationSkuIds1 =
                    // DetachedCriteria.forClass(InventoryImpl.class)
                    // .add(Restrictions.eq("fulfillmentLocation.id",
                    // locationId))
                    // .setProjection(Projections.property("sku.id"));
                    // return
                    // Subqueries.propertyNotIn(targetPropertyName,
                    // locationSkuIds);

                    CriteriaQuery<Long> q = builder.createQuery(Long.class);
                    Root<InventoryImpl> subRoot = q.from(InventoryImpl.class);
                    q.where(builder.equal(subRoot.get("fulfillmentLocation.id"), locationId));
                    q.select(subRoot.<Long>get("sku.id"));
                    return builder.not(explicitPath.in(q)); // FIXME
                }
            }));
    filterMappings.add(f);
}

From source file:org.apache.ranger.service.XTrxLogService.java

public Long searchXTrxLogsCount(SearchCriteria searchCriteria) {
    EntityManager em = daoMgr.getEntityManager();
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(VXXTrxLog.class);
    Root<VXXTrxLog> rootEntityType = selectCQ.from(VXXTrxLog.class);
    Predicate predicate = generatePredicate(searchCriteria, em, criteriaBuilder, rootEntityType);

    CriteriaQuery<Long> countCQ = criteriaBuilder.createQuery(Long.class);
    countCQ.select(criteriaBuilder.count(rootEntityType)).where(predicate);
    List<Long> countList = em.createQuery(countCQ).getResultList();
    Long count = 0L;//www  .j  ava2 s  .  c  o  m
    if (!CollectionUtils.isEmpty(countList)) {
        count = countList.get(0);
        if (count == null) {
            count = 0L;
        }
    }
    return count;
}

From source file:eu.uqasar.service.ProcessService.java

/**
 * /*from w w w  .  j av a 2s.co m*/
 * @param processId
 * @return
 */
public boolean processExists(Long processId) {
    logger.info(String.format("checking if process with ID %d exists ...", processId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    criteria.where(cb.equal(from.get(Process_.id), processId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Page<Order> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Order>(Collections.<Order>emptyList(), 0, pageable);
    }//w  ww  . ja v a  2  s.co m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public List<Order> findList(Member member, Integer count, List<Filter> filters,
        List<net.groupbuy.Order> orders) {
    if (member == null) {
        return Collections.<Order>emptyList();
    }//w w w . j  a v  a  2 s. c om
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchNewestAccount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    List<Account> candidates = entityManager
            .createQuery(q.select(account).orderBy(cb.desc(account.get(Account_.id)))).setMaxResults(1)
            .getResultList();/*from  w w w  . j  a  va2  s.  c o m*/

    if (candidates.isEmpty()) {
        return null;
    } else {
        return candidates.get(0);
    }
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long waitingShippingCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled),
            criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.paid),
            criteriaBuilder.equal(root.get("shippingStatus"), ShippingStatus.unshipped));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }/*from ww  w.  j av  a 2 s.  com*/
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:org.broadleafcommerce.openadmin.server.security.dao.AdminPermissionDaoImpl.java

@Override
public AdminPermission readAdminPermissionByName(String name) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
    Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
    criteria.select(adminPerm);

    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(adminPerm.get("name"), name));

    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<AdminPermission> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<AdminPermission> results = query.getResultList();
    if (results == null || results.size() == 0) {
        return null;
    } else {//from w  w  w  . j  ava2 s. c o m
        return results.get(0);
    }
}