Example usage for javax.persistence.criteria CriteriaQuery where

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

Introduction

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

Prototype

CriteriaQuery<T> where(Predicate... restrictions);

Source Link

Document

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.

Usage

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

/**
 * {@inheritDoc}//w ww  .  j ava 2  s.c  om
 */
public long getCustomerCreditCardsCount(long userId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<CreditCard> root = query.from(CreditCard.class);
    query.select(builder.count(root));

    if (CommonUtil.isNotNull(userId)) {
        User user = userRepository.findById(userId);
        if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) {
            query.where(builder.equal(root.<Long>get(FIELDCONSTANT_CARDHOLDERID), user.getId()));
        }
    }
    return entityManager.createQuery(query).getSingleResult();
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoArvoDAOImpl.java

@Override
public YhteystietoArvo findByOrganisaatioAndNimi(String organisaatioOid, String nimi) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoArvo> query = cb.createQuery(YhteystietoArvo.class);

    Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class);
    query.select(root);/*from   w w w . ja  v  a  2  s. com*/

    Predicate organisaatioEquals = cb.equal(root.get("organisaatio").get("oid"), organisaatioOid);
    Predicate nameEquals = cb.equal(root.get("kentta").get("nimi"), nimi);

    Predicate whereClause = cb.and(organisaatioEquals, nameEquals);
    query.where(whereClause);

    return getEntityManager().createQuery(query).getSingleResult();

    //        Organisaatio org = organisaatioDAO.findBy("oid", organisaatioOid).get(0);
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoArvo x " +
    //                "WHERE x.kentta.nimi = :nimi AND x.organisaatio.id = :organisaatioId");
    //        query.setParameter("nimi", nimi);
    //        query.setParameter("organisaatioId", org.getId());
    //        return (YhteystietoArvo) query.getSingleResult();
}

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

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {/*from  ww  w.j a  v a 2s.c  om*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletCookieDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.nowParameter = this.createParameterExpression(DateTime.class, "now");

    this.deletePortalCookieQueryString = "DELETE FROM " + PortalCookieImpl.class.getName() + " e " + "WHERE e."
            + PortalCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName();

    this.deleteEmptyPortalCookieQueryString = "DELETE FROM " + PortalCookieImpl.class.getName() + " e "
            + "WHERE e." + PortalCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName() + " AND "
            + "e." + PortalCookieImpl_.portletCookies.getName() + " IS EMPTY";

    this.deletePortletCookieQueryString = "DELETE FROM " + PortletCookieImpl.class.getName() + " e "
            + "WHERE e." + PortletCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName();

    this.findExpiredByParentPortletCookiesQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletCookieImpl>>() {
                @Override/*  w  w  w.  j a  va 2s.  c  om*/
                public CriteriaQuery<PortletCookieImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletCookieImpl> criteriaQuery = cb
                            .createQuery(PortletCookieImpl.class);
                    final Root<PortletCookieImpl> typeRoot = criteriaQuery.from(PortletCookieImpl.class);
                    criteriaQuery.select(typeRoot);
                    criteriaQuery.where(cb.lessThanOrEqualTo(
                            typeRoot.get(PortletCookieImpl_.portalCookie).get(PortalCookieImpl_.expires),
                            nowParameter));

                    return criteriaQuery;
                }
            });
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public List<Member> findAllCreatedMembersByFromAndToDate(Date fromDate, Date toDate) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);
    Path<Date> dateCreatedPath = member.get("createdDate");
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(cb.lessThanOrEqualTo(dateCreatedPath, toDate));
    predicates.add(cb.greaterThanOrEqualTo(dateCreatedPath, fromDate));
    criteria.where(predicates.toArray(new Predicate[predicates.size()]));
    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java

@Override
public List<YhteystietoElementti> findByLisatietoIdAndKentanNimi(String yhteystietojenTyyppiOid,
        String kentanNimi) {//from www  . j a  v  a  2 s . com

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class);

    Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class);
    query.select(root);

    Predicate tyyppiEquals = cb.equal(root.get("yhteystietojenTyyppi").get("oid"), yhteystietojenTyyppiOid);
    Predicate nameEquals = cb.equal(root.get("nimi"), kentanNimi);

    Predicate whereClause = cb.and(tyyppiEquals, nameEquals);
    query.where(whereClause);

    return getEntityManager().createQuery(query).getResultList();

    //        YhteystietojenTyyppi ytT =  yttDao.findBy("oid", yhteystietojenTyyppiOid).get(0);
    //        Query query = getEntityManager().createQuery(
    //                "SELECT ltk FROM YhteystietoElementti ltk " +
    //                "WHERE ltk.yhteystietojenTyyppi.id = :yhteystietojenTyyppiId " +
    //                "AND ltk.nimi = :kentanNimi"
    //                );
    //        return query
    //                .setParameter("yhteystietojenTyyppiId", ytT.getId())
    //                .setParameter("kentanNimi", kentanNimi)
    //                .getResultList();
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public List<ActivityAIPC> itemsDateRange(Date start, Date end, int[] range) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));
    cq.orderBy(cb.asc(rt.get(ActivityAIPC_.fromdateofservice)));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);/*from ww w  .j a  v  a  2 s  .  co  m*/
    return q.getResultList();

}

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

/**
 * Adds the filter criteria./*  w w w  . ja  v a  2  s . co m*/
 *
 * @param filters
 *            the filters
 * @param query
 *            the query
 */
private void addFilterCriteria(Map<String, Object> filters, CriteriaBuilder builder, Root<BulkUpload> root,
        CriteriaQuery query) {
    if (!filters.isEmpty()) {
        List<Predicate> predicates = new ArrayList<Predicate>();
        for (Entry<String, Object> entry : filters.entrySet()) {
            if (CommonUtil.isNotNull(root.get(entry.getKey()))) {
                predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO));
            }
        }
        query.where(predicates.toArray(new Predicate[] {}));
    }
}

From source file:org.apache.ambari.server.orm.dao.ServiceConfigDAO.java

@RequiresSession
public List<ServiceConfigEntity> getLastServiceConfigVersionsForGroups(Collection<Long> configGroupIds) {
    if (configGroupIds == null || configGroupIds.isEmpty()) {
        return Collections.emptyList();
    }/*from ww w.  j  a  va2  s .c om*/
    CriteriaBuilder cb = entityManagerProvider.get().getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = cb.createTupleQuery();
    Root<ServiceConfigEntity> groupVersion = cq.from(ServiceConfigEntity.class);

    cq.multiselect(groupVersion.get("groupId").alias("groupId"),
            cb.max(groupVersion.<Long>get("version")).alias("lastVersion"));
    cq.where(groupVersion.get("groupId").in(configGroupIds));
    cq.groupBy(groupVersion.get("groupId"));
    List<Tuple> tuples = daoUtils.selectList(entityManagerProvider.get().createQuery(cq));
    List<ServiceConfigEntity> result = new ArrayList<ServiceConfigEntity>();
    //subquery look to be very poor, no bulk select then, cache should help here as result size is naturally limited
    for (Tuple tuple : tuples) {
        CriteriaQuery<ServiceConfigEntity> sce = cb.createQuery(ServiceConfigEntity.class);
        Root<ServiceConfigEntity> sceRoot = sce.from(ServiceConfigEntity.class);

        sce.where(cb.and(cb.equal(sceRoot.get("groupId"), tuple.get("groupId")),
                cb.equal(sceRoot.get("version"), tuple.get("lastVersion"))));
        sce.select(sceRoot);
        result.add(daoUtils.selectSingle(entityManagerProvider.get().createQuery(sce)));
    }

    return result;
}

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));/*ww  w. j  a  va 2 s .  co m*/
    count.where(builder.equal(root.get("id"), id));
    return entityManager.createQuery(count).getSingleResult() == 1;
}