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:net.shopxx.dao.impl.OrderDaoImpl.java

public Long completeOrderCount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/* w  ww. ja va 2  s . c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:eu.uqasar.service.AbstractService.java

/**
 * Method to get the entity of class by a given name.
 * @param clazz Class/*from  w w w .jav a 2s. com*/
 * @param name attribute name of the searched entity
 * @return T entity
 */
public <T extends Persistable> T getByName(Class<T> clazz, String name) {
    logger.infof("loading %s with name %d ...", getReadableClassName(clazz), name);
    T entity = null;

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz);
    Root<T> ent = criteriaQuery.from(clazz);
    criteriaQuery.select(ent).where(criteriaBuilder.equal(ent.get("name"), name));

    List<T> entities = em.createQuery(criteriaQuery).getResultList();

    if (entities != null && entities.size() > 0) {
        entity = entities.get(0);
    }
    return entity;
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

@Override
public boolean exists(Serializable id) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> count = builder.createQuery(Long.class);
    Root<?> root = count.from(getEntityClass());
    count.select(builder.count(root));//from   w  ww.j  a  v  a2s.c o  m
    count.where(builder.equal(root.get("id"), id));
    return entityManager.createQuery(count).getSingleResult() == 1;
}

From source file:dk.dma.msinm.service.CategoryService.java

/**
 * Looks up an category by name// w  ww .  ja va 2 s .c  o  m
 * @param name the name to search for
 * @param lang the language. Optional
 * @param parentId the parent ID. Optional
 * @return The matching category, or null if not found
 */
public Category findByName(String name, String lang, Integer parentId) {
    // Sanity check
    if (StringUtils.isBlank(name)) {
        return null;
    }

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Category> categoryQuery = builder.createQuery(Category.class);

    Root<Category> categoryRoot = categoryQuery.from(Category.class);

    // Build the predicate
    PredicateHelper<Category> predicateBuilder = new PredicateHelper<>(builder, categoryQuery);

    // Match the name
    Join<Category, CategoryDesc> descs = categoryRoot.join("descs", JoinType.LEFT);
    predicateBuilder.like(descs.get("name"), name);
    // Optionally, match the language
    if (StringUtils.isNotBlank(lang)) {
        predicateBuilder.equals(descs.get("lang"), lang);
    }

    // Optionally, match the parent
    if (parentId != null) {
        categoryRoot.join("parent", JoinType.LEFT);
        Path<Category> parent = categoryRoot.get("parent");
        predicateBuilder.equals(parent.get("id"), parentId);
    }

    // Complete the query
    categoryQuery.select(categoryRoot).distinct(true).where(predicateBuilder.where());

    // Execute the query and update the search result
    List<Category> result = em.createQuery(categoryQuery).getResultList();

    return result.size() > 0 ? result.get(0) : null;
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

/**
 * /*from w  w  w .ja v a 2s  .c  om*/
 * @return
 */
protected CriteriaQuery<E> createCriteriaQuery() {
    final CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<E> criteria = builder.createQuery(getEntityClass());
    criteria.select(criteria.from(getEntityClass()));
    return criteria;
}

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

public BigDecimal createOrderAmount(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("amount")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }//from  w  w  w.  j a v a  2  s . co  m
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult();
    return result != null ? result : BigDecimal.ZERO;
}

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

public BigDecimal completeOrderAmount(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("amount")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate));
    }//ww  w.j a  va 2 s  .c  o m
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult();
    return result != null ? result : BigDecimal.ZERO;
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

@Override
public Long countAll() {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> count = builder.createQuery(Long.class);
    count.select(builder.count(count.from(getEntityClass())));
    return entityManager.createQuery(count).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  ww . j a  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:com.sfs.ucm.controller.HelpContentAction.java

/**
 * load resources//from   w w  w .j  a  va 2 s .c om
 * 
 * @throws UCMException
 */
private void loadList() throws UCMException {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Help> c = cb.createQuery(Help.class);
        Root<Help> obj = c.from(Help.class);
        c.select(obj).orderBy(cb.asc(obj.get("keyword")));
        this.helpItems = em.createQuery(c).getResultList();

        this.itemCount = this.helpItems.size();
    } catch (Exception e) {
        throw new UCMException(e);
    }
}