List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:org.businessmanager.dao.GenericDaoImpl.java
@Override public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult, int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) { CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder, rootQuery);/*from w ww . j ava 2 s . co m*/ select.where(predicateList.toArray(new Predicate[0])); if (orderAsc) { criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute))); } else { criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute))); } TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery); if (firstResult != -1) { typedQuery.setFirstResult(firstResult); } if (maxResults != -1) { typedQuery.setMaxResults(maxResults); } return typedQuery.getResultList(); }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
public Predicate getWherePredicate(Class<T> clazz, EntityManagerFactory emf) throws Exception { // CriteriaBuilder CriteriaBuilder criteriaBuilder = emf.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz); Root<T> root = criteriaQuery.from(clazz); criteriaQuery.select(root); List<Predicate> predicateList = new ArrayList<Predicate>(); // return getWherePredicates(emf, predicateList, criteriaQuery, // criteriaBuilder, root); Predicate[] predicates = getWherePredicates(emf, predicateList, criteriaQuery, criteriaBuilder, root); Predicate wherePredicate = criteriaBuilder.and(predicates); return wherePredicate; }
From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java
public Long findFilteredCount(String search, List<String> searchProperties) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<ENTITY> from = query.from(entityClass); query.select(builder.count(from)); if (!Strings.isNullOrEmpty(search)) { Predicate[] buildFilterPredicates = BuildFilterPredicates(from, search, searchProperties); if (buildFilterPredicates.length > 0) { query.where(builder.or(buildFilterPredicates)); }/*from ww w . ja v a2 s. c om*/ } return count(entityManager.createQuery(query)); }
From source file:eu.uqasar.service.AbstractService.java
public <T extends Persistable> long countAll(Class<T> clazz) { logger.infof("counting all %ss ...", getReadableClassName(clazz)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<T> root = criteria.from(clazz); criteria.select(cb.countDistinct(root)); return em.createQuery(criteria).getSingleResult(); }
From source file:eu.uqasar.service.AbstractService.java
/** * * @return/* w ww . j ava2 s . co m*/ */ public long countAll() { logger.infof("counting all %ss ...", readableClassName); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<T> root = criteria.from(clazz); criteria.select(cb.countDistinct(root)); return em.createQuery(criteria).getSingleResult(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * in and path:/*from ww w. j ava2 s . com*/ * SELECT e FROM jpa_query_employee e WHERE e.address.state IN ('NY','MI') */ @Transactional public void doWhere3() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // method 1 c.select(e).where(e.get("address").get("state").in("NY", "MI")); showResult(c); // method 2 c.select(e).where(cb.in(e.get("address").get("state")).value("NY").value("MI")); showResult(c); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/*from w ww . j av a2 s .c om*/ public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.select(productJoin); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
public CriteriaQuery<T> getCriteria(Class<T> clazz, EntityManagerFactory emf) throws Exception { // CriteriaBuilder CriteriaBuilder criteriaBuilder = emf.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz); Root<T> root = criteriaQuery.from(clazz); criteriaQuery.select(root); // criteria.where( builder.equal( personRoot.get( Utente_.eyeColor ), // "brown" ) ); List<Predicate> predicateList = new ArrayList<Predicate>(); // buildWhere(emf, predicateList, criteriaQuery, criteriaBuilder, root); ////from ww w . ja va2 s . c o m // Predicate[] predicates = new Predicate[predicateList.size()]; // predicateList.toArray(predicates); Predicate[] predicates = getWherePredicates(emf, predicateList, criteriaQuery, criteriaBuilder, root); Predicate wherePredicate = criteriaBuilder.and(predicates); criteriaQuery.where(wherePredicate); // buildSort(criteriaQuery, criteriaBuilder, root); List<Order> orders = new ArrayList<Order>(); buildSort(orders, criteriaQuery, criteriaBuilder, root); if (ListUtils.isNotEmpty(orders)) criteriaQuery.orderBy(orders); return criteriaQuery; }
From source file:org.synyx.hades.dao.orm.GenericJpaDao.java
/** * Creates a new count query for the given {@link Specification}. * //from w w w. j av a 2 s . c om * @param spec can be {@literal null}. * @return */ private TypedQuery<Long> getCountQuery(Specification<T> spec) { CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<T> root = applySpecificationToCriteria(spec, query); query.select(builder.count(root)).distinct(true); return getEntityManager().createQuery(query); }
From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletEntityDao.java
@Override public void afterPropertiesSet() throws Exception { this.userIdParameter = this.createParameterExpression(Integer.class, "userId"); this.portletDefinitionParameter = this.createParameterExpression(PortletDefinitionImpl.class, "portletDefinition"); this.findEntitiesForDefinitionQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletEntityImpl>>() { @Override/*from w w w .j av a 2 s.c o m*/ public CriteriaQuery<PortletEntityImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletEntityImpl> criteriaQuery = cb .createQuery(PortletEntityImpl.class); final Root<PortletEntityImpl> entityRoot = criteriaQuery.from(PortletEntityImpl.class); criteriaQuery.select(entityRoot); addFetches(entityRoot); criteriaQuery.where(cb.equal(entityRoot.get(PortletEntityImpl_.portletDefinition), portletDefinitionParameter)); return criteriaQuery; } }); this.findEntitiesForUserIdQuery = this .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletEntityImpl>>() { @Override public CriteriaQuery<PortletEntityImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PortletEntityImpl> criteriaQuery = cb .createQuery(PortletEntityImpl.class); final Root<PortletEntityImpl> entityRoot = criteriaQuery.from(PortletEntityImpl.class); criteriaQuery.select(entityRoot); addFetches(entityRoot); criteriaQuery.where(cb.equal(entityRoot.get(PortletEntityImpl_.userId), userIdParameter)); return criteriaQuery; } }); }