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.AbstractService.java

protected Long performDistinctCountWithEqualPredicate(final Attribute<T, ?> attr, Object value) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<T> root = query.from(clazz);
    query.where(cb.equal(root.get(attr.getName()), value));
    query.select(cb.countDistinct(root));
    return em.createQuery(query).getSingleResult();
}

From source file:com.samples.platform.core.SystemUserInitDao.java

/**
 * Get the {@link AuthenticationType}s out of the database.
 *
 * @param enabled/*from  w  ww .j a v a 2  s  .c o m*/
 *            if not <code>null</code> and <code>true</code> only the
 *            enabled {@link AuthenticationType}s are replied.
 * @return the list of {@link AuthenticationType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void enterSystemUser(final String contextName, final String userName, final String password,
        final String... roleNames) {
    AuthenticationType ac = this.of.createAuthenticationType();
    ac.setContext(contextName);
    ac.setEnabled(true);
    GrantedAuthorityType r;
    for (String roleName : roleNames) {
        r = this.of.createGrantedAuthorityType();
        r.setRoleName(roleName);
        ac.getGrantedAuthority().add(r);
    }
    ac.setPassword(password);
    ac.setUserName(userName);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    Predicate ands = cb.conjunction();
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), contextName));
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.userName), userName));
    q.where(ands);
    q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName)));
    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    try {
        AuthenticationType stored = typedQuery.getSingleResult();
        if (stored != null) {
            this.em.persist(ac);
        }
    } catch (NoResultException e) {
        this.em.persist(ac);
    }
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute//  ww w.j a va 2s  .  c o m
public long countByExample(final T example, final T example2) {
    OpenJPACriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);
    Root<T> from = q.from(persistentClass);
    final Expression<Long> count = newCountExpression(cb, from);
    q.select(count);
    q.where(newQbePredicates(cb, from, example, example2));
    TypedQuery<Long> query = entityManager.createQuery(q);
    try {
        return query.getSingleResult();
    } catch (NoResultException e) {
        return 0;
    }
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/* w  w  w.  ja  v a2 s . c  o m*/
public long countByFullTexts(final String terms) {
    LOGGER.debug("counting by text {} terms {}", persistentClass, terms);
    if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) {
        return 0;
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);

    Root<T> p = q.from(persistentClass);
    final Expression<Long> count = newCountExpression(cb, p);
    q.select(count);
    q.where(newFullTextPredicate(cb, p, terms));

    try {
        return entityManager.createQuery(q).getSingleResult();
    } catch (NoResultException e) {
        return 0;
    }
}

From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java

@Override
public List<PurchaseOrder> findAll() {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();

    Root<PurchaseOrder> root = cq.from(PurchaseOrder.class);
    cq.select(root);/*  w  w w.  j  a  v  a 2 s .  c  o  m*/

    ParameterExpression<String> status = cb.parameter(String.class);

    //        cq.where(cb.notEqual(invRoot.get("status"), status));
    cq.where(cb.notEqual(root.get("deleted"), 'Y'));
    cq.orderBy(cb.desc(root.get("purchaseOrderId")));
    Query query = getEntityManager().createQuery(cq);
    //        query.setParameter(status, PaymentStatusEnum.DELETE.getValue());

    List<PurchaseOrder> list = query.getResultList();

    for (PurchaseOrder wo : list) {
        List<PurchaseOrderItems> tempDel = new ArrayList<>();
        for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) {
            if (wot.isDeleted()) {
                tempDel.add(wot);
            }
        }
        wo.getPurchaseOrderItems().removeAll(tempDel);
    }
    return list;
}

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

public List<T> getAllExcept(Collection<T> toExclude) {
    if (toExclude == null || toExclude.isEmpty()) {
        return this.getAll();
    }//from  ww w.j a  v a 2s .com
    logger.infof("loading all %s except %s ...", readableClassName, toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(this.clazz);
    Root<T> root = query.from(this.clazz);
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:org.openmeetings.app.data.user.dao.UsersDaoImpl.java

public List<Users> getAllUsers() {
    try {/*from   w w  w.j  a  va2  s  .c o  m*/
        // get all non-deleted users
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Users> cq = cb.createQuery(Users.class);
        Root<Users> c = cq.from(Users.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        TypedQuery<Users> q = em.createQuery(cq);
        List<Users> ll = q.getResultList();

        return ll;
    } catch (Exception ex2) {
        log.error("[getAllUsers] ", ex2);
    }
    return null;
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Override
public List<ENTITY> find(Collection<KEY> ids) {
    if (log.isDebugEnabled()) {
        log.debug("Finding by ids " + entityClass + "#" + ids);
    }/*  w  w  w.  java  2  s .  co m*/
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<ENTITY> criteriaQuery = cb.createQuery(entityClass);
    Root<ENTITY> from = criteriaQuery.from(entityClass);
    criteriaQuery.where(from.in(ids));
    TypedQuery<ENTITY> query = entityManager.createQuery(criteriaQuery);
    return findList(query);
}

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

@Override
public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult,
        int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

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

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);/* w  w w.  ja  v  a2s  .c  o  m*/
    select.where(predicateList.toArray(new Predicate[0]));

    if (orderAsc) {
        criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute)));
    } else {
        criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute)));
    }

    TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);

    if (firstResult != -1) {
        typedQuery.setFirstResult(firstResult);
    }
    if (maxResults != -1) {
        typedQuery.setMaxResults(maxResults);
    }

    return typedQuery.getResultList();
}

From source file:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java

@Override
public OpenHubProjectEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubProjectEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubProjectEntity> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.select(root);/*from  w w w. j av  a  2s.c  o  m*/

    Predicate predicate = criteriaBuilder.equal(root.get("name"), name);
    criteriaQuery.where(predicate);

    TypedQuery<OpenHubProjectEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubProjectEntity projectEntity = query.getSingleResult();

    return projectEntity;
}