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:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

private Query applicationServersForSuggestBox(String input) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> q = cb.createQuery(String.class);
    Root<ResourceEntity> r = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType");
    Predicate appServerNamePred = cb.like(resType.<String>get("name"),
            DefaultResourceTypeDefinition.APPLICATIONSERVER.name());
    input = input + "%";
    q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input)));
    q.select(r.<String>get("name"));
    q.distinct(true);/*from   w  w  w . j  av a2  s.c o m*/

    q.orderBy(cb.asc(r.get("name")));

    return entityManager.createQuery(q);
}

From source file:org.broadleafcommerce.openadmin.server.security.dao.AdminPermissionDaoImpl.java

@Override
public AdminPermission readAdminPermissionByNameAndType(String name, String type) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
    Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
    criteria.select(adminPerm);/*  www .  ja  v a 2 s .  c  o m*/

    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(adminPerm.get("name"), name));
    restrictions.add(builder.equal(adminPerm.get("type"), type));

    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<AdminPermission> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<AdminPermission> results = query.getResultList();
    if (results == null || results.size() == 0) {
        return null;
    } else {
        return results.get(0);
    }
}

From source file:eu.uqasar.service.user.UserService.java

public User getByFullNameWithUsername(final String fullNameWithUsername) {
    logger.infof("loading User with fullName and username %s ...", fullNameWithUsername);
    User entity = null;/*from   www  .j  a  v  a2  s.c  o m*/
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Expression<String> fullName = cb.concat(cb.concat(root.get(User_.firstName), " "),
            root.get(User_.lastName));
    Expression<String> userName = cb.concat(" (", cb.concat(root.get(User_.userName), ")"));
    query.where(cb.equal(cb.lower(cb.concat(fullName, userName)), fullNameWithUsername.toLowerCase()));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;
}

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

/**
 * //from  w w w  . j  a  va  2  s.c  o  m
 * @param adapter
 * @return
 */
public int countAllByAdapter(AdapterSettings adapter) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter);
    query.where(condition);
    return em.createQuery(query).getResultList().size();
}

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

/**
 * /*from  w w  w .j a v  a 2s .  co m*/
 * @param adapter
 * @return
 */
public List<JiraMetricMeasurement> getAllByAdapter(AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter);
    query.where(condition);
    return em.createQuery(query).getResultList();
}

From source file:org.apache.rave.portal.repository.impl.JpaWidgetRepository.java

@Override
public int getCountByStatusAndTypeAndFreeText(WidgetStatus widgetStatus, String type, String searchTerm) {
    final CriteriaBuilder cb = manager.getCriteriaBuilder();
    final CriteriaQuery<Long> query = cb.createQuery(Long.class);
    final Root<JpaWidget> widgetType = query.from(JpaWidget.class);
    query.select(cb.count(widgetType));/*  ww w.  java  2 s .co  m*/
    query.where(getStatusAndTypeAndFreeTextPredicates(cb, widgetType, widgetStatus, type, searchTerm));

    final Long countResult = manager.createQuery(query).getSingleResult();
    return countResult.intValue();
}

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

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

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

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

public Sessiondata getSessionByHash(String SID) {
    try {/* w w  w  . j  a  va  2 s. 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 null;
        }

        Sessiondata sd = fullList.get(0);

        return sd;
    } catch (Exception ex2) {
        log.error("[updateUser]: ", ex2);
    }
    return null;
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

public List<T> findByAssignedEntity(SetAttribute<T, ?> setAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(setAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(listAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}