Example usage for javax.persistence.criteria CriteriaQuery from

List of usage examples for javax.persistence.criteria CriteriaQuery from

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery from.

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:net.przemkovv.sphinx.dao.impl.DefaultSolutionDAO.java

@Override
public List<Solution> getSolutionsByTask(Task task) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Solution> cq = cb.createQuery(Solution.class);
    Root<Solution> solution = cq.from(Solution.class);
    cq.where(cb.equal(solution.get(Solution_.task), task));
    return em.createQuery(cq).getResultList();
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.DBInitApplicationListener.java

protected boolean isDBEmpty() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    cq.select(cb.count(cq.from(ClaimEntity.class)));

    return em.createQuery(cq).getSingleResult() == 0;
}

From source file:cz.vse.webmail.EmailDAOBean.java

@Override
public List<Email> getFilteredEmails(User user, EmailListFilter filter) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Email> criteria = cb.createQuery(Email.class);
    Root<Email> root = criteria.from(Email.class);
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(cb.equal(root.<User>get("owner"), user));
    if (StringUtils.isNoneBlank(filter.getTo())) {
        predicates.add(cb.like(root.<String>get("to"), "%" + filter.getTo() + "%"));
    }/*from ww  w.  j  a  v a 2  s.co  m*/
    criteria.select(root).where(predicates.toArray(new Predicate[] {}));
    return entityManager.createQuery(criteria).getResultList();
}

From source file:com.juliuskrah.multipart.repository.impl.HibernateAccountRepository.java

@Override
public Stream<Account> findByLastName(String lastName) {
    Session session = this.em.unwrap(Session.class);
    CriteriaBuilder qb = session.getCriteriaBuilder();
    CriteriaQuery<Account> cq = qb.createQuery(Account.class);
    Root<Account> root = cq.from(Account.class);
    Predicate predicate = qb.like(root.get(Account_.lastName), lastName);
    cq.where(predicate);/*from w  ww .  ja  v  a  2s .  co m*/

    return session.createQuery(cq).stream();
}

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

public Page<Deposit> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Deposit>(Collections.<Deposit>emptyList(), 0, pageable);
    }/*w w w .  ja  v  a2 s.  co m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Deposit> criteriaQuery = criteriaBuilder.createQuery(Deposit.class);
    Root<Deposit> root = criteriaQuery.from(Deposit.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:cz.muni.expense.data.UserRepository.java

public User findByUsername(String username) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> user = criteria.from(User.class);
    criteria.select(user).where(cb.equal(user.get(User_.username), username));
    try {//from   w  w  w  .  j av a  2s . c  om
        return em.createQuery(criteria).getSingleResult();
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:cz.muni.expense.data.UserRepository.java

public User findByUsernameAndAuthToken(String authId, String authToken) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> user = criteria.from(User.class);
    criteria.select(user).where(cb.equal(user.get(User_.username), authId));
    try {//from   w w  w  .j  a  va  2s. c o  m
        User userToReturn = em.createQuery(criteria).getSingleResult();
        return userToReturn.getAuthToken().equals(authToken) ? userToReturn : null;
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:com.juliuskrah.multipart.repository.impl.HibernateAccountRepository.java

@Override
public Optional<Account> findByUsername(String username) {
    CriteriaBuilder qb = this.em.getCriteriaBuilder();
    CriteriaQuery<Account> query = qb.createQuery(Account.class);
    Root<Account> root = query.from(Account.class);
    Predicate predicate = qb.like(root.get(Account_.username), username);
    query.where(predicate);//from  ww w  .  j  a v a2 s  .  c  om
    Query q = this.em.createQuery(query);

    return Optional.of((Account) q.getSingleResult());
}

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(//from   w w w  .j  a  v a2s  .  c  om
            cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));

    for (Account each : em.createQuery(query).getResultList()) {
        em.remove(each);
    }
}