Example usage for javax.persistence.criteria CriteriaBuilder equal

List of usage examples for javax.persistence.criteria CriteriaBuilder equal

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder equal.

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

From source file:com.expressui.sample.dao.query.ContactQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Contact> query,
        Root<Contact> contact) {/*  w  ww.j a  v a2s. c  o  m*/
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(lastName)) {
        ParameterExpression<String> lastNameExp = builder.parameter(String.class, "lastName");
        predicates.add(builder.like(builder.upper(contact.<String>get("lastName")), lastNameExp));
    }
    if (hasValue(account)) {
        ParameterExpression<Account> accountExp = builder.parameter(Account.class, "account");
        predicates.add(builder.equal(contact.get("account"), accountExp));
    }

    return predicates;
}

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

/**
 * /*from   w  w  w .ja v  a  2 s .c o  m*/
 * @param adapter
 * @return
 */
public List<JiraMetricMeasurement> getAllByAdapter(AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter);
    query.where(condition);
    return em.createQuery(query).getResultList();
}

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

/**
 * /*from www.  j  a v a2  s  . co  m*/
 * @param first
 * @param count
 * @param adapter
 * @return
 */
public List<JiraMetricMeasurement> getAllByAdapter(int first, int count, AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter);
    query.orderBy(cb.asc(root.get(JiraMetricMeasurement_.jiraKey)));
    query.where(condition);
    return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList();
}

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

/**
 * /* w  w  w  .  ja v a  2s .  c  om*/
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<JiraMetricMeasurement> getMeasurementsForMetric(String metric) throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    query.where(condition);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

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

/**
 * //from  w w w .j av  a 2  s .com
 * @param metric
 * @return
 * @throws uQasarException
 */
private List<JiraMetricMeasurement> getMeasurementsByMetricAndDate(String metric, Date date)
        throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

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

public Page<Order> findPage(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//from  www.j a  v  a  2s .com
    Predicate restrictions = criteriaBuilder.conjunction();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public Long count(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired) {//from w ww .  j a v a2  s .  c  o  m
    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();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

/**
 * //from w w w  . j a va2  s  .  c o  m
 * @param metric
 * @param date
 * @return
 * @throws uQasarException
 */
public int countMeasurementsByMetricAndDate(String metric, Date date) throws uQasarException {
    logger.info("Count measurements for metric: " + metric + " and date: " + date);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    Integer res = em.createQuery(query).getResultList().size();
    logger.info("Results' count: " + res);
    return res;
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Get all {@link SystemUserLogType}s of the day of the application.
 *
 * @param date/*from w ww .  j a  va2s  .co m*/
 *            the date the calls are recorded.
 * @return the list of {@link SystemUserLogType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<SystemUserLogType> getSystemUserLogType(final Date date) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    Date d = date;
    if (d == null) {
        d = new Date();
    }
    final CriteriaQuery<SystemUserLogType> q = cb.createQuery(SystemUserLogType.class);
    final Root<SystemUserLogType> c = q.from(SystemUserLogType.class);
    q.where(cb.equal(c.<String>get(SystemUserLogType_.context), this.contextNameProvider.getContextName()),
            cb.between(c.<Date>get(SystemUserLogType_.logDateItem), getDayStart(d), getDayEnd(d)));

    final TypedQuery<SystemUserLogType> typedQuery = this.em.createQuery(q);
    return typedQuery.getResultList();
}

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

public BigDecimal getSalesAmount(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("amountPaid")));
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed));
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }/* w w  w  .j a v  a2 s. c  om*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}