Example usage for javax.persistence.criteria CriteriaBuilder createQuery

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

Introduction

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

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

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

/**
 * Creates a {@link javax.persistence.TypedQuery} for the given {@link org.springframework.data.jpa.domain.Specification} and {@link org.springframework.data.domain.Sort}.
 *
 * @param spec can be {@literal null}./*from   ww  w .j  a  va 2s . co m*/
 * @param sort can be {@literal null}.
 * @return
 */
private TypedQuery<M> getQuery(Specification<M> spec, Sort sort) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<M> query = builder.createQuery(entityClass);

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

    applyJoins(root);

    if (sort != null) {
        query.orderBy(toOrders(sort, root, builder));
    }

    TypedQuery<M> q = em.createQuery(query);

    repositoryHelper.applyEnableQueryCache(q);

    return applyLockMode(q);
}

From source file:com.luna.common.repository.support.SimpleBaseRepository.java

/**
 * Creates a {@link javax.persistence.TypedQuery} for the given {@link org.springframework.data.jpa.domain.Specification} and {@link org.springframework.data.domain.Sort}.
 *
 * @param spec can be {@literal null}.//from www. j ava2s  .  co m
 * @param sort can be {@literal null}.
 * @return
 */
protected TypedQuery<M> getQuery(Specification<M> spec, Sort sort) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<M> query = builder.createQuery(entityClass);

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

    applyJoins(root);

    if (sort != null) {
        query.orderBy(toOrders(sort, root, builder));
    }

    TypedQuery<M> q = em.createQuery(query);

    repositoryHelper.applyEnableQueryCache(q);

    return applyLockMode(q);
}

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 va 2 s.  c  o 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:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

/**
 * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}.
 * /*from  w w w .  j av a2s  . co m*/
 * @param spec can be {@literal null}.
 * @param sort can be {@literal null}.
 * @return
 */
protected TypedQuery<M> getQuery(Specification<M> spec, Sort sort) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<M> query = builder.createQuery(getDomainClass());

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

    if (sort != null) {
        query.orderBy(toOrders(sort, root, builder));
    }

    return applyRepositoryMethodMetadata(em.createQuery(query));
}

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

/**
 * /*from  www.  java  2s  .c  o m*/
 * @return
 */
public List<CubesMetricMeasurement> getAllCubesMetricObjects() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    query.from(CubesMetricMeasurement.class);
    return em.createQuery(query).getResultList();
}

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//  w  w w.  ja  v  a2 s .  c o 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<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:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * Count the measurement results belonging to a specific adapter
 * @param adapter/*from  w w  w .  j a v a  2s  . co m*/
 * @return
 */
public int countAllByAdapter(AdapterSettings 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.where(condition);
    return em.createQuery(query).getResultList().size();
}

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

/**
 * Get the latest date of measurement snapshots
 * @return/*from   w  ww.  ja v a 2s .  c o  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:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * /*from  ww  w  .j  ava 2  s .co m*/
 * @param adapter
 * @return
 */
public List<CubesMetricMeasurement> getAllByAdapter(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.where(condition);
    return em.createQuery(query).getResultList();
}

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

/**
 * /*  w ww . j  av a2 s. c om*/
 * @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();
}