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:de.ks.idnadrev.information.view.InformationOverviewDS.java

protected List<Tag> getTags(List<String> tagNames, EntityManager em) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tag> query = builder.createQuery(Tag.class);
    Root<Tag> root = query.from(Tag.class);
    Path<String> namePath = root.get(KEY_NAME);
    query.select(root);/*from   www. j av a2 s. com*/
    query.where(namePath.in(tagNames));

    return em.createQuery(query).getResultList();
}

From source file:com.aimdek.ccm.dao.impl.test.BasicAbstractGenericDaoImplTest.java

/**
 * Find by id.//from ww  w.  j a v  a2 s .  c o m
 *
 * @param id
 *            the id
 * @param entityClass
 *            the entity class
 * @return the entity type
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get("id"), id));

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

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

/**
 * {@inheritDoc}//w ww .j a v  a 2 s  . co m
 */
public List<User> getCustomers() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.select(root);
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    return entityManager.createQuery(query).getResultList();
}

From source file:com.aimdek.ccm.dao.impl.test.StatementDaoImplTest.java

/**
 * Test filtering.//  www . j a v  a 2 s.  com
 */
@Test
public void testFiltering() {

    String cardNumber = "0008";
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statement> query = builder.createQuery(Statement.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(root);
    query.where(builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), "%" + cardNumber));
    List<Statement> statementList = entityManager.createQuery(query).getResultList();

    assertEquals(4, statementList.size());
}

From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java

/**
 * Returns a unsorted list of distinct values of the requested field, filtered using a {@link QueryCriteria}
 * based query.//w w w. j  av  a2 s.c o m
 *
 * @param field          Model field name.
 * @param queryCriterias Query criteria to filter the field values by.
 * @return Sorted list of distinct values of {@code field}.
 */
public Iterable<Object> distinct(String field, Iterable<QueryCriteria> queryCriterias) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType());
    Root<T> root = query.from(metadata.getJavaType());
    query.where(queryBuilder.createSpecification(queryCriterias).toPredicate(root, query, builder));
    query.select(root.get(field)).distinct(true);
    return (List) entityManager.createQuery(query).getResultList();
}

From source file:org.sloth.persistence.impl.CategorieDaoImpl.java

@Override
public Categorie getByTitle(String title) {
    if (title == null) {
        throw new NullPointerException();
    }/*from   w  w  w .j  a  v  a  2 s .c  o  m*/
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Categorie> cq = cb.createQuery(Categorie.class);
    Root<Categorie> categorie = cq.from(Categorie.class);
    cq.select(categorie);
    cq.where(cb.equal(categorie.get(Categorie_.title), title));
    Categorie result = null;
    try {
        result = getEntityManager().createQuery(cq).getSingleResult();
    } catch (NoResultException e) {
        logger.info("Categorie with Title {} not found", title);
    } catch (NonUniqueResultException e) {
        logger.warn("Corrupt Database", e);
    }
    return result;
}

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

/**
 * //from  w  ww.  j ava 2s .  com
 * @param processId
 * @return
 */
public boolean processExists(Long processId) {
    logger.info(String.format("checking if process with ID %d exists ...", processId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    criteria.where(cb.equal(from.get(Process_.id), processId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

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

/**
 * {@inheritDoc}// ww w.java  2  s .  c  o  m
 */
public User findLastCustomer() {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.select(root);
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last customer", e);
    }
    return null;
}

From source file:net.groupbuy.dao.impl.ReceiverDaoImpl.java

public Page<Receiver> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Receiver> criteriaQuery = criteriaBuilder.createQuery(Receiver.class);
    Root<Receiver> root = criteriaQuery.from(Receiver.class);
    criteriaQuery.select(root);/*from  w  w w  .  j av a 2  s. c o m*/
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }
    return super.findPage(criteriaQuery, pageable);
}

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

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public List<E> findAll(final PredicateBuilder<E> filter) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    criteriaQuery.where(filter.createPredicate(builder, from));

    return this.find(criteriaQuery);
}