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.groupbuy.dao.impl.CouponCodeDaoImpl.java

public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class);
    Root<CouponCode> root = criteriaQuery.from(CouponCode.class);
    criteriaQuery.select(root);//from   w  ww. jav a 2  s . c o m
    Predicate restrictions = criteriaBuilder.conjunction();
    Path<Coupon> couponPath = root.get("coupon");
    if (coupon != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon));
    }
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (hasBegun != null) {
        if (hasBegun) {
            restrictions = criteriaBuilder.and(restrictions,
                    criteriaBuilder.or(couponPath.get("beginDate").isNull(),
                            criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date())));
        } else {
            restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(),
                    criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date()));
        }
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(couponPath.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions,
                    criteriaBuilder.or(couponPath.get("endDate").isNull(),
                            criteriaBuilder.greaterThanOrEqualTo(couponPath.<Date>get("endDate"), new Date())));
        }
    }
    if (isUsed != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

/**
 * //from ww w  .  ja v a 2s  . co m
 * @param first
 * @param count
 * @return
 */
public List<Process> getAllByAscendingName(int first, int count) {
    logger.infof("loading all Processes ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    criteria.orderBy(cb.asc(root.get(Process_.name)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java

/**
 * {@inheritDoc}// w  ww . j  a v a 2  s.  com
 */
public long getStatementsCount(Map<String, Object> filters, long userId) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(builder.count(root));
    addFilterCriteria(userId, filters, builder, root, query);

    return entityManager.createQuery(query).getSingleResult();
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchAccountWithPrincipalName(String principalName) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    try {/*from   www.j a v  a 2 s .  c  om*/
        return entityManager
                .createQuery(
                        q.select(account).where(cb.equal(account.get(Account_.principalName), principalName)))
                .getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:core.commonapp.server.dao.party.PersonDaoHibernateImpl.java

@Override
@Transactional//from  w  ww  .  ja v  a 2 s  .co m
public List<Person> findAllContactPeople(Integer partyId) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Person> query = builder.createQuery(Person.class);

    Root<Person> contactPerson = query.from(Person.class);
    Join<Person, PartyRelationship> partyToRelationships = contactPerson.join("partyToRelationships");

    query.where(builder.equal(partyToRelationships.get("partyFrom.partyId"), partyId),
            builder.isNull(partyToRelationships.get("thruDate")));

    List<Person> people = getEntityManager().createQuery(query).getResultList();

    PartyDaoHibernateImpl.lazyLoad((Party) people, true, true, false, false);

    return people;
}

From source file:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public <E extends Entity<K>, K> List<E> findAll(final Class<E> clazz) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(clazz);
    criteriaQuery.from(clazz);

    return this.find(criteriaQuery);
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchNewestAccount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    List<Account> candidates = entityManager
            .createQuery(q.select(account).orderBy(cb.desc(account.get(Account_.id)))).setMaxResults(1)
            .getResultList();//from w ww  .jav a2 s  . c  o m

    if (candidates.isEmpty()) {
        return null;
    } else {
        return candidates.get(0);
    }
}

From source file:com.movies.dao.impl.BaseDaoImpl.java

@Override
public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass,
        List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes,
        List<SetAttribute> setAttributes) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true);
    Root<T> root = cq.from(returnClass);

    if (CollectionUtils.isNotEmpty(singleAttributes)) {
        for (SingularAttribute attribute : singleAttributes) {
            root.join(attribute);/* w  w  w. j  a va2s  . c  o  m*/
            root.fetch(attribute);
        }
    }

    if (CollectionUtils.isNotEmpty(listAttributes)) {
        for (ListAttribute attribute : listAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }

    if (CollectionUtils.isNotEmpty(setAttributes)) {
        for (SetAttribute attribute : setAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }
    Set<Entry<String, Object>> set = map.entrySet();
    int numberOfClauses = set.size();
    Predicate[] predicates = new Predicate[numberOfClauses];
    int i = 0;
    for (Entry<String, Object> entry : set) {
        String key = entry.getKey();
        if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) {
            predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE);
        } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) {
            //predicates[i++] = cb.equal( ,entry.getValue());
        } else {
            predicates[i++] = cb.equal(root.get(key), entry.getValue());
        }
    }

    return em.createQuery(cq.select(root).where(predicates)).getResultList();

}

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

public List<Process> getAllByAscendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from  ww w . ja v a 2  s  . com*/

    criteria.orderBy(cb.asc(root.get(Process_.endDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public List<Process> getAllByDescendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from w  w w.j  a  v a  2s. c  om*/

    criteria.orderBy(cb.desc(root.get(Process_.endDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}