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:cz.muni.fi.pa165.bookingmanager.managers.UserManager.java

@Override
public List<User> findAll() {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<User> query = criteriaBuilder.createQuery(User.class);
    Root<User> c = query.from(User.class);
    query.select(c);/*from ww  w.  j av  a  2  s  .com*/

    TypedQuery<User> typedQuery = entityManager.createQuery(query);

    return typedQuery.getResultList();
    //return em.createQuery("SELECT u FROM User u WHERE u.user = :user", User.class).setParameter("user", user).getResultList();
}

From source file:org.businessmanager.dao.security.GroupDaoImpl.java

@Override
public Group findGroupByName(String name) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Group> query = queryBuilder.createQuery(Group.class);
    Root<Group> groupQuery = query.from(Group.class);
    query.select(groupQuery).where(queryBuilder.equal(groupQuery.get(Group_.name), name));

    try {//from   ww w .  ja  va 2  s  . c  o  m
        return getEntityManager().createQuery(query).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

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

public List<Parameter> findList(ParameterGroup parameterGroup, Set<Parameter> excludes) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Parameter> criteriaQuery = criteriaBuilder.createQuery(Parameter.class);
    Root<Parameter> root = criteriaQuery.from(Parameter.class);
    criteriaQuery.select(root);//from ww  w.j  a  va 2s . c o  m
    Predicate restrictions = criteriaBuilder.conjunction();
    if (parameterGroup != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("parameterGroup"), parameterGroup));
    }
    if (excludes != null && !excludes.isEmpty()) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes)));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

From source file:org.hellospring4.dao.AbstractDAO.java

public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return entityManager.createQuery(cq).getResultList();
}

From source file:org.jboss.spring.quickstarts.kitchensink.kitchensink_spring.repo.MemberDaoImpl.java

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

    /*// www  .  j  ava2  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(builder.equal(member.get("email"), email));
    return em.createQuery(criteria).getSingleResult();
}

From source file:$.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);

        /*//from  w  ww  .  j  a v  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:$.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 w  w  w  .ja  v  a2s . co 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();
    }

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional// w  w w  .  j  a  v a  2 s .c o m
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:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.PersistentDocumentStoreController.java

/**
 * Gets all UUIDs for {@code T} type document stores owned by the given owner.
 * @param em the {@link EntityManager} to use.
 * @param ownerId the ID of the owner.//from   w ww.  j a  v  a2s.  co m
 * @param entityClass the specific type of document stores to be retrieved; must be annotated with the {@link Entity} annotation.
 * @return a {@link List} containing {@link String} representations of the UUIDs.
 */
public <T extends PersistentDocumentStore> List<String> getAllUuids(EntityManager em, String ownerId,
        Class<T> entityClass) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(ownerId, CannedMessages.NULL_ARGUMENT, "ownerId");
    Validate.notNull(entityClass, CannedMessages.NULL_ARGUMENT, "entityClass");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class);
    Root<T> store = cq.from(entityClass);
    cq.multiselect(store.get("id"))
            .where(cb.equal(store.get("ownerId"), cb.parameter(String.class, "ownerId")));
    TypedQuery<byte[]> tq = em.createQuery(cq);
    tq.setParameter("ownerId", ownerId);
    return Lists.newArrayList(Iterables.transform(tq.getResultList(), UuidUtils.uuidBytesToStringFunction()));
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional//from   ww  w.  j  ava 2s  . c  o m
public void testCountFromComplexCriteria() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> bookQuery = cb.createQuery(Book.class);
    Root<Book> root = bookQuery.from(Book.class);
    Join<Book, Category> join = root
            .join(em.getMetamodel().entity(Book.class).getSingularAttribute("category", Category.class));
    bookQuery.where(cb.equal(join.<String>get("name"), "Java"));
    bookQuery.from(Author.class);
    bookQuery.select(root);
    CriteriaQuery<Long> countQuery = JpaUtils.countCriteria(em, bookQuery);
    Long result = Long.valueOf(em.createQuery(bookQuery).getResultList().size());

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