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.excilys.ebi.sample.jpa.query.benchmark.repository.impl.JPACriteriaRepository.java

@Override
public List<Song> getSongsByArtistNameOrderBySongTitle(final String name) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Song> query = builder.createQuery(Song.class);
    Root<Song> root = query.from(Song.class);

    query.where(builder.equal(root.get(Song_.artist).get(Artist_.name), name))
            .orderBy(builder.asc(root.get(Song_.title)));

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

From source file:example.springdata.jpa.showcase.snippets.AccountRepositoryImpl.java

@Override
public void removedExpiredAccounts(LocalDate reference) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Account> query = cb.createQuery(Account.class);
    Root<Account> account = query.from(Account.class);

    query.where(
            cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));

    for (Account each : em.createQuery(query).getResultList()) {
        em.remove(each);/*from   w  ww  .j  av a2 s  .  c o  m*/
    }
}

From source file:org.oncoblocks.centromere.jpa.test.EntrezGeneRepositoryImpl.java

public List<EntrezGene> guessGene(String keyword) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntrezGene> query = builder.createQuery(EntrezGene.class);
    Root<EntrezGene> root = query.from(EntrezGene.class);
    query.where(builder.equal(root.get("primaryGeneSymbol"), keyword));
    List<EntrezGene> genes = entityManager.createQuery(query).getResultList();
    if (genes != null && genes.size() > 0)
        return genes;

    query = builder.createQuery(EntrezGene.class);
    root = query.from(EntrezGene.class);
    Path join = root.join("aliases");
    query.where(builder.equal(join.get("name"), keyword));
    return entityManager.createQuery(query).getResultList();
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional/*from w  w  w  . java 2  s . c  om*/
public void testInitialize() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Category> c = cb.createQuery(Category.class);
    Root<Category> root = c.from(Category.class);
    c.where(cb.equal(JpaUtils.getPath(root, "name"), "Java"));
    List<Category> list = em.createQuery(c).getResultList();
    Category cat = list.get(0);
    JpaUtils.initialize(em, cat, 2);
    cat.getBooks().contains(new Book());
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional/*w w w . j  ava2  s . com*/
public void testCountFromCriteria() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> bookQuery = cb.createQuery(Book.class);
    Root<Book> root = bookQuery.from(Book.class);
    bookQuery.where(cb.equal(JpaUtils.getPath(root, "author.name"), "Rod"));
    CriteriaQuery<Long> countQuery = JpaUtils.countCriteria(em, bookQuery);
    Long result = Long.valueOf(em.createQuery(bookQuery).getResultList().size());

    assertEquals(result, (Long) em.createQuery(countQuery).getSingleResult());

}

From source file:com.toptal.dao.UserDaoImpl.java

@Override
public final User findByName(final String name) {
    final CriteriaBuilder builder = this.manager.getCriteriaBuilder();
    final CriteriaQuery<User> query = builder.createQuery(User.class);
    final Root<User> root = query.from(User.class);
    query.where(builder.equal(root.get(NAME_FIELD), name));
    return this.getSingleResult(query);
}

From source file:core.commonapp.server.dao.security.UserLoginDaoHibernateImpl.java

@Override
public boolean existByUsername(String username) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserLogin> query = builder.createQuery(UserLogin.class);

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

    query.where(builder.equal(root.get("username"), username));

    return getEntityManager().createQuery(query).getResultList().size() > 0;
}

From source file:core.commonapp.server.dao.security.UserLoginDaoHibernateImpl.java

@Override
public List<UserLogin> findAllLikeUsername(String username) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserLogin> query = builder.createQuery(UserLogin.class);

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

    query.where(builder.like(root.<String>get("username"), "%" + username + "%"));

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

From source file:core.commonapp.server.dao.security.PermissionSecurityGroupDaoHibernateImpl.java

@Override
public List<PermissionSecurityGroup> findAllBySecurityGroup(SecurityGroup securityGroup, boolean includeOld) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<PermissionSecurityGroup> query = builder.createQuery(getPersistClass());
    Root<PermissionSecurityGroup> root = query.from(getPersistClass());
    query.where(builder.equal(root.get("securityGroup"), securityGroup));

    if (!includeOld) {
        query.where(builder.isNull(root.get("thruDate")));
    }//w ww . j a  va  2 s .c  om

    return (List<PermissionSecurityGroup>) getEntityManager().createQuery(query).getResultList();
}

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

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

    Root<OpenHubLicenseEntity> root = criteriaQuery.from(getEntityClass());
    CriteriaQuery<OpenHubLicenseEntity> select = criteriaQuery.select(root);

    Predicate namePredicate = criteriaBuilder.equal(root.get("name"), name);
    select.where(namePredicate);

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

    OpenHubLicenseEntity result = null;//w w  w .  ja  v a2  s . c  o m

    try {
        result = query.getSingleResult();
    } catch (NoResultException ex) {

    } catch (NonUniqueResultException ex) {

    }

    return result;
}