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:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

/**
 * Retrieves the distinct UUIDs of the available requesting facilities.
 *//*  w  w w  .  ja  v  a 2 s .co m*/
@Override
public List<UUID> getRequestingFacilities(List<UUID> supplyingFacilityIds) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<UUID> query = builder.createQuery(UUID.class);
    Root<Order> root = query.from(Order.class);

    if (!isEmpty(supplyingFacilityIds)) {
        Predicate predicate = builder.disjunction();
        for (Object elem : supplyingFacilityIds) {
            predicate = builder.or(predicate, builder.equal(root.get(SUPPLYING_FACILITY_ID), elem));
        }
        query.where(predicate);
    }

    query.select(root.get(REQUESTING_FACILITY_ID)).distinct(true);

    return entityManager.createQuery(query).getResultList();
}

From source file:com.sishuok.es.common.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}./*  ww w . j a v a  2 s. co  m*/
 * @return
 */
private TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * SELECT e FROM jpa_query_employee e WHERE e.name= :name
 *//*from   w  w w. j av a2s.  c o  m*/
@Transactional
public void doParameterQuery() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // parameter, equal to ":name"
    ParameterExpression<String> paraName = cb.parameter(String.class, "name");

    // e.name = ":name"
    c.select(e).where(cb.equal(e.get("name"), paraName));

    // set param value
    TypedQuery<Employee> query = em.createQuery(c);
    query.setParameter("name", "employee_1");
    List<Employee> result = query.getResultList();
    ResultViewer.showResult(result, "criteria query : parameter query");
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * Get the latest date of measurement snapshots
 * @return/*from w  ww .  j  av  a  2  s  . co  m*/
 */
private Date getLatestDate() {
    logger.info("Get the latest date from CUBES measurements...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    query.select(root);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).setMaxResults(1).getSingleResult().getTimeStamp();
}

From source file:org.apache.rave.portal.repository.impl.JpaWidgetRepository.java

@Override
public int getCountByStatusAndTypeAndFreeText(WidgetStatus widgetStatus, String type, String searchTerm) {
    final CriteriaBuilder cb = manager.getCriteriaBuilder();
    final CriteriaQuery<Long> query = cb.createQuery(Long.class);
    final Root<JpaWidget> widgetType = query.from(JpaWidget.class);
    query.select(cb.count(widgetType));
    query.where(getStatusAndTypeAndFreeTextPredicates(cb, widgetType, widgetStatus, type, searchTerm));

    final Long countResult = manager.createQuery(query).getSingleResult();
    return countResult.intValue();
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}.//  ww  w  .  j av a2 s.  c  o  m
 * @return
 */
protected TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * load specifications/*from  ww  w  .  ja v a  2  s. c o m*/
 */
private void loadList() throws UCMException {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Specification> c = cb.createQuery(Specification.class);
    Root<Specification> obj = c.from(Specification.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.specifications = em.createQuery(c).getResultList();

}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *//*from w w w.  jav  a2s  .  c om*/
private List<SpecificationTest> findSpecificationTests(Specification specification) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationTest> c = cb.createQuery(SpecificationTest.class);
    Root<SpecificationTest> obj = c.from(SpecificationTest.class);
    c.select(obj).where(cb.equal(obj.get("specification"), specification));
    return em.createQuery(c).getResultList();
}

From source file:com.sfs.captor.controller.AlternativeFlowAction.java

/**
 * Get count of flowstep rules/*from  w  ww  .  jav  a  2s.  c  om*/
 * 
 * @return count
 */
private Long getFlowStepRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(FlowStepRule.class)));
    return em.createQuery(c).getSingleResult();
}

From source file:eu.uqasar.service.user.UserService.java

public long countAllFiltered(final UserFilterStructure filter) {
    logger.infof("counting all Users matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<User> from = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//w  w  w .  j  ava  2 s .  c om
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}