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:com.ocs.dynamo.functional.dao.DomainDaoImpl.java

public List<? extends Domain> findAllByType(Class<? extends Domain> type) {
    CriteriaQuery<? extends Domain> cq = getEntityManager().getCriteriaBuilder().createQuery(type);
    cq.from(type);
    return getEntityManager().createQuery(cq).getResultList();
}

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 {//  w  w  w . j ava  2 s . c  om
        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:net.carinae.dev.async.dao.DummyEntityDaoJPA2.java

@Override
public List<DummyEntity> findByData(String data) {

    // select de from DummyEntity
    //  where de.data = data
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<DummyEntity> cq = cb.createQuery(DummyEntity.class);
    Root<DummyEntity> de = cq.from(DummyEntity.class);
    cq.select(de).where(cb.equal(de.get(DummyEntity_.data), data));

    return this.entityManager.createQuery(cq).getResultList();
}

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

public List<FriendLink> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FriendLink> criteriaQuery = criteriaBuilder.createQuery(FriendLink.class);
    Root<FriendLink> root = criteriaQuery.from(FriendLink.class);
    criteriaQuery.select(root);/*from  ww w. j av 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:net.groupbuy.dao.impl.NavigationDaoImpl.java

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

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();
}

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

@Override
public List<Permission> getPermissionsForUser(User user) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Permission> cq = cb.createQuery(Permission.class);
    Root<Permission> permissions = cq.from(Permission.class);
    Join<Permission, Role> roles = permissions.join(Permission_.roles);
    Join<Role, User> users = roles.join(Role_.users);
    cq.where(cb.equal(users.get(User_.email), user.getEmail()));
    return em.createQuery(cq).getResultList();

}

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:br.com.compositeam.data.MemberDaoImpl.java

public Member findByEmail(String email) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*/*w  w w  . j av a2  s  .  c o  m*/
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

    criteria.select(member).where(cb.equal(member.get("email"), email));
    return em.createQuery(criteria).getSingleResult();
}

From source file:br.com.compositeam.data.MemberDaoImpl.java

public List<Member> findAllOrderedByName() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*/*from   ww  w .  jav  a2 s.  c  o m*/
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}