Example usage for javax.persistence.criteria CriteriaQuery from

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

Introduction

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

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

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

/**
 * /*from w w  w.  j  av a 2s . com*/
 * @param first
 * @param count
 * @return
 */
public List<CubesMetricMeasurement> getAllByAscendingName(int first, int count) {
    logger.infof("loading all CubesMetricMeasurement ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> criteria = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = criteria.from(CubesMetricMeasurement.class);
    criteria.orderBy(cb.asc(root.get(CubesMetricMeasurement_.self)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

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

/**
 * //w  w  w.  j av  a2  s .c  o m
 * @param first
 * @param count
 * @param adapter
 * @return
 */
public List<CubesMetricMeasurement> getAllByAdapter(int first, int count, AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

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

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

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

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

From source file:org.querybyexample.jpa.GenericRepository.java

/**
 * Count the number of E instances./*from   w w  w .j ava  2s  .  co m*/
 *
 * @param entity a sample entity whose non-null properties may be used as
 * search hint
 * @param searchParameters carries additional search information
 * @return the number of entities matching the search.
 */
@Transactional(readOnly = true)
public int findCount(E entity, SearchParameters sp) {
    checkNotNull(entity, "The entity cannot be null");
    checkNotNull(sp, "The searchParameters cannot be null");

    if (sp.hasNamedQuery()) {
        return byNamedQueryUtil.numberByNamedQuery(sp).intValue();
    }
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
    Root<E> root = criteriaQuery.from(type);

    if (sp.getDistinct()) {
        criteriaQuery = criteriaQuery.select(builder.countDistinct(root));
    } else {
        criteriaQuery = criteriaQuery.select(builder.count(root));
    }

    // predicate
    Predicate predicate = getPredicate(root, builder, entity, sp);
    if (predicate != null) {
        criteriaQuery = criteriaQuery.where(predicate);
    }

    orderByUtil.forceJoinOrder(root, sp);

    TypedQuery<Long> typedQuery = entityManager.createQuery(criteriaQuery);

    applyCacheHints(typedQuery, sp);
    Long count = typedQuery.getSingleResult();

    if (count != null) {
        return count.intValue();
    } else {
        log.warn("findCount returned null");
        return 0;
    }
}

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

/**
 * @param project//from   ww  w  .  j  av a2  s.c om
 * @return List with all the measurements of a project
 */
public List<CubesMetricMeasurement> getMeasurementByProject(Project project) {
    logger.info("Obtaining measurements for the project: " + project);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.project), project);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();

}

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

/**
 * /*from   w  ww .  j  a  v a2  s.c om*/
 * @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<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList().size();
}

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

/**
 * //  w  w  w.  j a  v a2s.  co m
 * @param metric
 * @return
 * @throws uQasarException
 */
private List<CubesMetricMeasurement> getMeasurementsByMetricAndDate(String metric, Date date)
        throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

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

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

/**
 * @param project/*from  w ww.j a v a 2s  .co  m*/
 * @param metric
 * @return
 */
public CubesMetricMeasurement getLatestMeasurementByProjectAndMetric(String project, String metric) {
    logger.info("Obtaining measurements for the project: " + project + " and metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubeName), project);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    List<CubesMetricMeasurement> measurements = em.createQuery(query).getResultList();
    CubesMetricMeasurement measurement = null;
    if (measurements != null && measurements.size() > 0 && measurements.get(0) != null) {
        measurement = measurements.get(0); // Get the most "fresh" result 
    }
    return measurement;
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

/**
 * update the session every time a user makes a request
 * //  www .j  a  va  2  s.c o m
 * @param SID
 */
private void updatesession(String SID) {
    try {
        // log.debug("****** updatesession: "+SID);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);

        List<Sessiondata> fullList = q.getResultList();
        if (fullList.size() == 0) {
            log.error("Found NO session to updateSession: ");

        } else {
            // log.debug("Found session to updateSession: ");
            Sessiondata sd = fullList.iterator().next();
            // log.debug("Found session to updateSession sd "+sd.getUser_id()+" "+sd.getSession_id());
            sd.setRefresh_time(new Date());

            if (sd.getId() == null) {
                em.persist(sd);
            } else {
                if (!em.contains(sd)) {
                    em.merge(sd);
                }
            }
        }

    } catch (Exception ex2) {
        log.error("[updatesession]: ", ex2);
    }
}

From source file:org.openmeetings.app.data.basic.Sessionmanagement.java

public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {//  www. ja  v  a 2s . c o m
        log.debug("updateUser User SID: " + SID);

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
        Root<Sessiondata> c = cq.from(Sessiondata.class);
        Predicate condition = cb.equal(c.get("session_id"), SID);
        cq.where(condition);

        TypedQuery<Sessiondata> q = em.createQuery(cq);
        List<Sessiondata> fullList = q.getResultList();

        if (fullList.size() == 0) {
            log.error("Could not find session to update: " + SID);
            return false;
        } else {
            // log.error("Found session to update: "+SID);
        }
        Sessiondata sd = fullList.get(0);
        // log.debug("Found session to update: "+sd.getSession_id()+
        // " userId: "+USER_ID);

        sd.setRefresh_time(new Date());
        sd.setSessionXml(sessionXml);

        if (sd.getId() == null) {
            em.persist(sd);
        } else {
            if (!em.contains(sd)) {
                em.merge(sd);
            }
        }
        // log.debug("session updated User: "+USER_ID);
        return true;
    } catch (Exception ex2) {
        log.error("[updateUserRemoteSession]: ", ex2);
    }
    return null;
}