List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
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: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 ww. j a va 2 s . c o m*/ 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:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}/*from w w w . j ava 2 s. c o m*/ */ public long getCustomersCount(Map<String, Object> filters) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<User> root = query.from(User.class); query.select(builder.count(root)); query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER)); this.addFilterCriteria(filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional/*from ww w. j a v a2s.c o m*/ public void testCountCriteria() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); Root<Book> root = criteria.from(Book.class); Join<Book, Author> join = root.join("author"); criteria.where(join.isNotNull()); CriteriaQuery<Long> countCriteria = JpaUtils.countCriteria(em, criteria); Long result = em.createQuery(countCriteria).getSingleResult(); log.debug("Count: " + result); }
From source file:de.egore911.persistence.selector.AbstractSelector.java
public long count() { EntityManager em = EntityManagerUtil.getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<T> from = cq.from(getEntityClass()); List<Predicate> predicates = generatePredicateList(builder, from, cq); cq.where(predicates.toArray(new Predicate[predicates.size()])); cq.select(builder.count(from));/* w w w .j av a2 s .co m*/ TypedQuery<Long> q = em.createQuery(cq); return q.getSingleResult(); }
From source file:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java
@SuppressWarnings("unchecked") private TypedQuery<T> constructTypedQueryByPropertyOrderBy(Class<? extends T> classe, String property, Object value, String orderByProperty) { CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery<T> c = (CriteriaQuery<T>) qb.createQuery(classe); Root<T> p = (Root<T>) c.from(classe); Predicate condition = qb.equal(p.get(property), value); c.where(condition); if (orderByProperty != null) { c.orderBy(qb.asc(p.get(orderByProperty))); }//from w w w . j a va 2s. c o m TypedQuery<T> q = em.createQuery(c); return q; }
From source file:de.egore911.persistence.selector.AbstractSelector.java
private TypedQuery<T> buildQuery() { EntityManager em = EntityManagerUtil.getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(getEntityClass()); Root<T> from = cq.from(getEntityClass()); List<Predicate> predicates = generatePredicateList(builder, from, cq); cq.where(predicates.toArray(new Predicate[predicates.size()])); cq.orderBy(generateOrderList(builder, from)); cq.select(from);/*from w w w . j av a 2 s .c om*/ return em.createQuery(cq); }
From source file:org.jboss.spring3_2.example.MatrixVariables.repo.MemberDaoImpl.java
public List<Member> findByNameAndEmail(String name, String email) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = builder.createQuery(Member.class); EntityType<Member> type = em.getMetamodel().entity(Member.class); Root<Member> member = criteria.from(Member.class); if (name != null && !name.isEmpty() && name != "") criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("name", String.class)), "%" + name + "%")); if (email != null && !email.isEmpty() && email != "") criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("email", String.class)), "%" + email + "%")); return em.createQuery(criteria).getResultList(); }
From source file:dao.jpa.JpaUtilsTest.java
@Test @Transactional/* ww w . j a va 2 s .com*/ 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:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java
@Override public List<YhteystietoElementti> findAllKaytossa() { // Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoElementti x where x.kaytossa = true"); // return query.getResultList(); CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class); Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class); query.select(root);//from ww w . j a v a 2s . c o m Predicate whereClause = cb.equal(root.get("kaytossa"), true); query.where(whereClause); return getEntityManager().createQuery(query).getResultList(); }