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:eu.uqasar.service.user.UserService.java

public User getByPWResetToken(final String token) {
    if (token == null || token.isEmpty()) {
        return null;
    }//from   ww  w.j  a  v a 2s .c om
    logger.infof("retrievung User with password reset token %s ...", token);
    User entity = null;
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.equal(root.get(User_.resetPWRequestToken), token));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static List<RunView> viewList(User user, String moduleResourceUri, Integer offset, Integer limit,
        String cloudServiceName) throws ConfigurationException, ValidationException {
    List<RunView> views = null;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {//from ww w. ja  va  2 s. com
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Run> critQuery = builder.createQuery(Run.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(rootQuery);
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        critQuery.orderBy(builder.desc(rootQuery.get("startTime")));
        TypedQuery<Run> query = em.createQuery(critQuery);
        if (offset != null) {
            query.setFirstResult(offset);
        }
        query.setMaxResults((limit != null) ? limit : DEFAULT_LIMIT);
        List<Run> runs = query.getResultList();
        views = convertRunsToRunViews(runs);
    } finally {
        em.close();
    }
    return views;
}

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

@Override
@Transactional(readOnly = true)// w  w w . j av a2  s  .  c  o  m
public List<ExampleGeneratedIdEntity> findByOtherText(final String text) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder.createQuery(this.entityClass);
    criteriaQuery.distinct(true);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);

    final ListJoin<ExampleGeneratedIdEntity, ExampleIdEntity> join = root
            .join(ExampleGeneratedIdEntity_.otherEntities);

    criteriaQuery.where(builder.equal(join.get(ExampleIdEntity_.text), text));

    return this.find(criteriaQuery);
}

From source file:org.openmeetings.app.data.basic.dao.LdapConfigDaoImpl.java

public List<LdapConfig> getLdapConfigs(int start, int max, String orderby, boolean asc) {
    try {/*from  w w w . j a v  a 2  s .  com*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<LdapConfig> cq = cb.createQuery(LdapConfig.class);
        Root<LdapConfig> c = cq.from(LdapConfig.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<LdapConfig> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<LdapConfig> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getLdapConfigs]", ex2);
    }
    return null;
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#unlockOperationOnServerStart()
 *///from   w  ww.ja  v a  2  s . c  o m
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void unlockOperationOnServerStart() {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("serverName"), this.hostName));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        List<OperationLockControllType> locks = typedQuery.getResultList();
        for (OperationLockControllType lock : locks) {
            this.logger.debug(" remove lock on server start (this {}): {} {}", this.hostName,
                    lock.getServerName(), lock.getLockDate());
            this.em.remove(this.em.merge(lock));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.carser.viamais.vo.TransactionFilter.java

public List<Transaction> resultList(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.where(getPredicates(cb, transaction));
    @SuppressWarnings("rawtypes")
    Path path;//from   ww  w .ja  va 2s . co  m
    switch (orderBy) {
    case CUSTOMER_NAME:
        path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name);
        break;
    case DATE:
        path = transaction.get(Transaction_.transactionDate);
        break;
    case PLATE:
        path = transaction.get(Transaction_.car).get(Car_.licensePlate);
        break;
    case TYPE:
        path = transaction.get(Transaction_.type);
        break;
    case YEAR:
        path = transaction.get(Transaction_.car).get(Car_.yearOfModel);
        break;
    default:
        path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name);
        break;
    }
    switch (order) {
    case DESC:
        cq.orderBy(cb.desc(path));
        break;
    default:
        cq.orderBy(cb.asc(path));
        break;
    }
    TypedQuery<Transaction> query = em.createQuery(cq);
    query.setFirstResult(offset);
    query.setMaxResults(limit);
    return query.getResultList();
}

From source file:org.apereo.portal.i18n.dao.jpa.JpaMessageDao.java

@Override
public void afterPropertiesSet() throws Exception {
    this.codeParameter = this.createParameterExpression(String.class, "code");
    this.localeParameter = this.createParameterExpression(Locale.class, "locale");

    this.findMessageByCodeAndLocaleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override//from w  w w  .ja  v a  2s. c o m
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.and(cb.equal(root.get(MessageImpl_.code), codeParameter),
                            cb.equal(root.get(MessageImpl_.locale), localeParameter)));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.code)),
                            cb.asc(root.get(MessageImpl_.locale)));

                    return criteriaQuery;
                }
            });

    this.findMessageByCodeQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.equal(root.get(MessageImpl_.code), codeParameter));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.locale)));

                    return criteriaQuery;
                }
            });

    this.findMessageByLocaleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.equal(root.get(MessageImpl_.locale), localeParameter));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.code)));

                    return criteriaQuery;
                }
            });

    this.findCodes = this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<String>>() {
        @Override
        public CriteriaQuery<String> apply(CriteriaBuilder cb) {
            final CriteriaQuery<String> criteriaQuery = cb.createQuery(String.class);
            final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
            criteriaQuery.select(root.get(MessageImpl_.code));
            criteriaQuery.groupBy(root.get(MessageImpl_.code));
            return criteriaQuery;
        }
    });
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static int viewListCount(User user, String moduleResourceUri, String cloudServiceName)
        throws ConfigurationException, ValidationException {
    int count = 0;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {/*from  w w w  .  j a  v a  2 s .c  o  m*/
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Long> critQuery = builder.createQuery(Long.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(builder.count(rootQuery));
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        TypedQuery<Long> query = em.createQuery(critQuery);
        count = (int) (long) query.getSingleResult();
    } finally {
        em.close();
    }
    return count;
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

@VisibleForTesting
int getNbEntriesBeforeSlicing(EntityManager entityManager, Class<E> entityClass,
        CriteriaBuilder criteriaBuilder, Predicate[] predicates) {

    CriteriaQuery<Long> counterQuery = criteriaBuilder.createQuery(Long.class);

    CriteriaQuery<Long> select = counterQuery.select(criteriaBuilder.count(counterQuery.from(entityClass)));

    if (predicates.length > 0) {
        select.where(predicates);
    }//ww  w.j  a  v a 2 s . c  om

    return entityManager.createQuery(counterQuery).getSingleResult().intValue();
}

From source file:fr.amapj.service.services.authentification.PasswordManager.java

/**
 * Retrouve l'utilisateur avec cet e-mail
 * Retourne null si non trouv ou autre problme
 *//*from w  w w  . j a  v a2  s.co  m*/
private Utilisateur findUser(String email, EntityManager em) {
    if ((email == null) || email.equals("")) {
        return null;
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();

    CriteriaQuery<Utilisateur> cq = cb.createQuery(Utilisateur.class);
    Root<Utilisateur> root = cq.from(Utilisateur.class);

    // On ajoute la condition where 
    cq.where(cb.equal(root.get(Utilisateur.P.EMAIL.prop()), email));

    List<Utilisateur> us = em.createQuery(cq).getResultList();
    if (us.size() == 0) {
        return null;
    }

    if (us.size() > 1) {
        logger.warn("Il y a plusieurs utilisateurs avec l'adresse " + email);
        return null;
    }

    return us.get(0);
}