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:eu.uqasar.service.dataadapter.JiraDataService.java

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

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project);

    Date from = getDateForPeriod(period);
    Date to = DateTime.now().toDate();
    Predicate condition3 = cb.between(root.get(JiraMetricMeasurement_.timeStamp), from, to);
    Predicate condition4 = cb.and(condition1, condition2, condition3);
    query.where(condition4);
    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.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> isManagedExternally(final Boolean isManagedExternally)
        throws ArgumentNullOrEmptyException {
    if (isManagedExternally == null) {
        throw new ArgumentNullOrEmptyException("isManagedExternally");
    }/* w  w  w  . jav a2 s.  com*/

    return new Specification<Device>() {

        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {
            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class);
            subquery.select(cb.countDistinct(deviceAuthorizationRoot));
            subquery.where(cb.equal(deviceAuthorizationRoot.get("device"), deviceRoot.<Long>get("id")));
            if (isManagedExternally) {
                return cb.greaterThan(subquery, Long.valueOf(1));
            } else {
                return cb.lessThanOrEqualTo(subquery, Long.valueOf(1));
            }
        }
    };
}

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

/**
 * Erase all {@link SystemUserLogType}s of the application scope older than
 * the given date.// w w w .j  a  va2 s. co  m
 *
 * @param toDate
 *            the date.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void eraseSystemUserLog(final Date toDate) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaDelete<SystemUserLogType> q = cb.createCriteriaDelete(SystemUserLogType.class);
    final Root<SystemUserLogType> c = q.from(SystemUserLogType.class);
    q.where(cb.lessThan(c.<Date>get(SystemUserLogType_.logDateItem), toDate),
            cb.equal(c.<String>get(SystemUserLogType_.context), this.contextNameProvider.getContextName()));
    try {
        this.em.createQuery(q).executeUpdate();
    } catch (final Exception e) {
        this.logger.error("eraseSystemUserLog: {}", e.getMessage());
    }
}

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

/**
 * Erase all {@link ApplicationUserLogType}s of the application scope older
 * than the given date.//from  ww w .  j a va  2  s  . c  o  m
 *
 * @param toDate
 *            the date.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void eraseApplicationUserLog(final Date toDate) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaDelete<ApplicationUserLogType> q = cb.createCriteriaDelete(ApplicationUserLogType.class);
    final Root<ApplicationUserLogType> c = q.from(ApplicationUserLogType.class);
    q.where(cb.lessThan(c.<Date>get(ApplicationUserLogType_.stopItem), toDate), cb
            .equal(c.<String>get(ApplicationUserLogType_.context), this.contextNameProvider.getContextName()));
    try {
        this.em.createQuery(q).executeUpdate();
    } catch (final Exception e) {
        this.logger.error("eraseApplicationUserLog: {}", e.getMessage());
    }
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private Product getProduct_Mercifully() {
    return transactionTemplate.execute(new TransactionCallback<Product>() {
        @Override//from w  w w  .  j ava2  s. com
        public Product doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> productRoot = query.from(Product.class);
            Join<Product, WarehouseProductInfo> warehouseProductInfoJoin = productRoot
                    .join(Product_.warehouseProductInfo);

            query.select(productRoot).where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"),
                    cb.gt(warehouseProductInfoJoin.get(WarehouseProductInfo_.quantity), 50)));
            return entityManager.createQuery(query).getSingleResult();
        }
    });
}

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

/** 
 * @param metric//from   ww w .j a  va  2 s  .c  om
 * @param project
 * @return
 * @throws uQasarException
 */
public List<JiraMetricMeasurement> getMeasurementsPerProjectByMetricWithLatestDate(Project project,
        String metric) throws uQasarException {
    logger.info("Count measurements for project + " + project.getAbbreviatedName() + " and metric: " + metric
            + "with latest date");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);

    Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project);

    Predicate condition4 = cb.and(condition1, condition2);
    query.where(condition4);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    List<JiraMetricMeasurement> returnResults = em.createQuery(query).getResultList();

    Date newDate = null;
    for (JiraMetricMeasurement jMM : returnResults) {
        if (newDate == null || jMM.getTimeStamp().compareTo(newDate) > 0) {
            newDate = jMM.getTimeStamp();
        }

    }

    return getMeasurementsByMetricAndDate(metric, newDate);
}

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

public Integer getSalesVolume(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity")));
    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 o m*/
    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();
}

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

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

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

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forOrganisation(final Organisation organisation)
        throws ArgumentNullOrEmptyException {
    if (organisation == null) {
        throw new ArgumentNullOrEmptyException("organisation");
    }/*w  w  w  .ja  va 2 s . com*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class);
            subquery.select(deviceAuthorizationRoot.get("device").get("id").as(Long.class));
            subquery.where(cb.equal(deviceAuthorizationRoot.get("organisation"), organisation.getId()));

            return cb.in(deviceRoot.get("id")).value(subquery);
        }
    };
}

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

/**
 * //from  ww w  .  jav  a 2 s .  c  om
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<JiraMetricMeasurement> getMeasurementsPerProjectByMetricWithinPeriod(Project project, String metric,
        String period) throws uQasarException {

    List<JiraMetricMeasurement> measurements = new ArrayList<>();
    if (project != null && metric != null && period != null) {
        logger.info(
                "Count measurements for project + " + project.getAbbreviatedName() + " and metric: " + metric);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
        Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);

        Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
        Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project);

        Date from = getDateForPeriod(period);
        Date to = DateTime.now().toDate();
        Predicate condition3 = cb.between(root.get(JiraMetricMeasurement_.timeStamp), from, to);
        //      System.out.println("from:"+from);
        //      System.out.println("to:"+to);

        Predicate condition4 = cb.and(condition1, condition2, condition3);
        query.where(condition4);
        query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
        measurements = em.createQuery(query).getResultList();
    }

    return measurements;
}