Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:com.github.springtestdbunit.entity.OtherEntityAssert.java

public void afterPropertiesSet() throws Exception {
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    this.criteriaQuery = cb.createQuery(OtherSampleEntity.class);
    Root<OtherSampleEntity> from = this.criteriaQuery.from(OtherSampleEntity.class);
    this.criteriaQuery.orderBy(cb.asc(from.get("value").as(String.class)));
}

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  .  j  a  v  a  2s .co m*/

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

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

public List<Tag> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tag> criteriaQuery = criteriaBuilder.createQuery(Tag.class);
    Root<Tag> root = criteriaQuery.from(Tag.class);
    criteriaQuery.select(root);//w w  w.  j  a  v  a  2s. c  o m
    if (type != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

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  www.ja  va2  s  .c o m*/
        return em.createQuery(criteria).getSingleResult();
    } 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);// www.  j  a v  a 2s . c om
    Query q = this.em.createQuery(query);

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

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 {/* w ww .j  a v a 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.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: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  .  j a  va 2 s  .  c o 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 findByUsernameAndPassword(String username, String password) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> user = criteria.from(User.class);
    // Swap criteria statements if you would like to try out type-safe criteria queries, a new
    // feature in JPA 2.0
    //criteria.select(member).where(cb.equal(member.get(Member_.name), email));
    criteria.select(user).where(cb.equal(user.get(User_.username), username));
    try {//from  w  ww . java2s. co m
        User userToReturn = em.createQuery(criteria).getSingleResult();
        String hash = org.apache.commons.codec.digest.DigestUtils.sha256Hex(password);
        return userToReturn.getPasswdHash().equals(hash) ? userToReturn : null;
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:com.excilys.ebi.sample.jpa.query.benchmark.repository.impl.JPACriteriaRepository.java

@Override
public List<Song> getSongsBySameArtistOrderBySongTitle(final Integer songId) {

    // select s.artist.songs from Song s where s.id=?

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

    Root<Song> song1 = query.from(Song.class);
    Join<Artist, Song> artistSongs = song1.join(Song_.artist).join(Artist_.songs);

    query.select(artistSongs).where(builder.equal(song1.get(Song_.id), songId))
            .orderBy(builder.asc(artistSongs.get(Song_.title)));
    return em.createQuery(query).getResultList();
}