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:dao.jpa.JpaUtilsTest.java

@Test
@Transactional//w  ww . j a v  a  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: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);/*w  w  w  . j  av  a  2  s .co 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.businessmanager.dao.security.PermissionDaoImpl.java

@Override
public Permission findPermissionByName(String name) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Permission> query = queryBuilder.createQuery(Permission.class);
    Root<Permission> permissionQuery = query.from(Permission.class);
    query.select(permissionQuery).where(queryBuilder.equal(permissionQuery.get(Permission_.name), name));

    try {//from  www. j a v  a 2s . co  m
        return getEntityManager().createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.nuevebit.persistence.repository.JPASearchableRepository.java

@Override
public long count(S searchCriteria) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);

    Root<T> p = createQueryRoot(q, searchCriteria);
    q.select(cb.count(p));// w  w w  . j  a  v a 2s .  c  o m

    return getEntityManager().createQuery(q).getSingleResult();
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional//from   w  ww. j av  a 2s  .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 w w . j  a  v a2s . com
 * @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:net.osgiliath.jpa.repository.impl.HelloJpaRepository.java

@Override
public Collection<? extends HelloEntity> findByHelloObjectMessage(String message_p) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<HelloEntity> cq = cb.createQuery(HelloEntity.class);
    Root<HelloEntity> helloObject = cq.from(HelloEntity.class);
    cq.select(helloObject);//from  w w  w.ja  va 2s.  c om
    Predicate where = cb.equal(helloObject.get("helloMessage"), message_p);
    cq.where(where);
    TypedQuery<HelloEntity> q = entityManager.createQuery(cq);
    List<HelloEntity> result = q.getResultList();
    return result;
}

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 . j  av  a2  s  . c o  m*/
        return getEntityManager().createQuery(query).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:dao.jpa.JpaUtilsTest.java

@Test
@Transactional//from  w  w  w .j  av a2  s  . co 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());
}

From source file:core.commonapp.server.dao.contact.PartyContactMechDaoHibernateImpl.java

@Override
public Set<PartyContactMech> findByContactMechId(Integer contactMechId) {
    log.debug("PartyContactMechDaoHibernateImpl.findByContactMechId({0})", contactMechId);

    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<PartyContactMech> query = builder.createQuery(PartyContactMech.class);
    Root<PartyContactMech> root = query.from(PartyContactMech.class);
    builder.equal(root.get("contactMechId"), contactMechId);
    builder.isNull(root.get("thruDate"));

    return (Set<PartyContactMech>) new HashSet(getEntityManager().createQuery(query).getResultList());
}