List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:eu.uqasar.service.ProcessService.java
/** * /*from w w w .jav a2 s . c o m*/ * @param processId * @return */ public boolean processExists(Long processId) { logger.info(String.format("checking if process with ID %d exists ...", processId)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Process> from = criteria.from(Process.class); criteria.where(cb.equal(from.get(Process_.id), processId)); criteria.select(cb.countDistinct(from)); return (em.createQuery(criteria).getSingleResult() == 1); }
From source file:org.jboss.quickstarts.wfk.contact.ContactRepository.java
/** * Find just one Contact by the first name that is passed in. If there is more then one, only the first will be returned. * // w ww .j a v a2 s . com * @param firstName * @return Contact */ Contact findByFirstName(String firstName) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Contact> criteria = cb.createQuery(Contact.class); Root<Contact> contact = criteria.from(Contact.class); // Swap criteria statements if you would like to try out type-safe criteria queries, a new feature in JPA 2.0. // criteria.select(contact).where(cb.equal(contact.get(Contact_.firstName), firstName)); criteria.select(contact).where(cb.equal(contact.get("firstName"), firstName)); return em.createQuery(criteria).getSingleResult(); }
From source file:org.jboss.quickstarts.wfk.contact.ContactRepository.java
/** * Find just one Contact by the last name that is passed in. If there is more then one, only the first will be returned. * /*from ww w. j a v a2 s . co m*/ * @param lastName * @return Contact */ Contact findByLastName(String lastName) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Contact> criteria = cb.createQuery(Contact.class); Root<Contact> contact = criteria.from(Contact.class); // Swap criteria statements if you would like to try out type-safe criteria queries, a new feature in JPA 2.0. // criteria.select(contact).where(cb.equal(contact.get(Contact_.lastName), lastName)); criteria.select(contact).where(cb.equal(contact.get("lastName"), lastName)); return em.createQuery(criteria).getSingleResult(); }
From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java
@Override public List<DeviceVO> list(String name, String namePattern, Long networkId, String networkName, Long deviceClassId, String deviceClassName, String sortField, @NotNull Boolean sortOrderAsc, Integer take, Integer skip, HivePrincipal principal) { final CriteriaBuilder cb = criteriaBuilder(); final CriteriaQuery<Device> criteria = cb.createQuery(Device.class); final Root<Device> from = criteria.from(Device.class); final Predicate[] predicates = CriteriaHelper.deviceListPredicates(cb, from, ofNullable(name), ofNullable(namePattern), ofNullable(networkId), ofNullable(networkName), ofNullable(deviceClassId), ofNullable(deviceClassName), ofNullable(principal)); criteria.where(predicates);//from www . j av a 2 s . com CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), sortOrderAsc); final TypedQuery<Device> query = createQuery(criteria); cacheQuery(query, of(CacheConfig.refresh())); ofNullable(take).ifPresent(query::setMaxResults); ofNullable(skip).ifPresent(query::setFirstResult); List<Device> resultList = query.getResultList(); return resultList.stream().map(Device::convertToVo).collect(Collectors.toList()); }
From source file:net.dontdrinkandroot.persistence.dao.TypedJpaDao.java
@Override @Transactional(propagation = Propagation.MANDATORY, readOnly = true) public List<E> findAll(final SingularAttribute<? super E, ?> attribute, final boolean asc, final int firstResult, final int maxResults) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass); final Root<E> from = criteriaQuery.from(this.entityClass); if (attribute != null) { if (asc) { criteriaQuery.orderBy(builder.asc(from.get(attribute))); } else {// w w w . j a v a 2 s.c o m criteriaQuery.orderBy(builder.desc(from.get(attribute))); } } return this.find(criteriaQuery, firstResult, maxResults); }
From source file:net.dontdrinkandroot.persistence.dao.TypedJpaDao.java
@Override @Transactional(propagation = Propagation.MANDATORY, readOnly = true) public List<E> findAll(final Collection<PredicateBuilder<E>> filters) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass); final Root<E> from = criteriaQuery.from(this.entityClass); final Predicate[] predicates = new Predicate[filters.size()]; int count = 0; for (final PredicateBuilder<E> filter : filters) { predicates[count] = filter.createPredicate(builder, from); count++;/* w w w. j a v a 2 s . c o m*/ } criteriaQuery.where(predicates); return this.find(criteriaQuery); }
From source file:net.dontdrinkandroot.persistence.dao.TypedJpaDao.java
@Override @Transactional(propagation = Propagation.MANDATORY, readOnly = true) public List<E> findAll(final PredicateBuilder<E>... filters) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass); final Root<E> from = criteriaQuery.from(this.entityClass); final Predicate[] predicates = new Predicate[filters.length]; int count = 0; for (final PredicateBuilder<E> filter : filters) { predicates[count] = filter.createPredicate(builder, from); count++;/*from w w w. j av a 2 s . c o m*/ } criteriaQuery.where(predicates); return this.find(criteriaQuery); }
From source file:eu.uqasar.service.ProcessService.java
public long countAllFiltered(final ProcessesFilterStructure filter) { logger.infof("counting all Processes matching the filter %s ...", filter); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Process> from = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, from); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//w w w . jav a 2 s.c o m criteria.select(cb.countDistinct(from)); return em.createQuery(criteria).getSingleResult(); }
From source file:com.sapito.db.dao.AbstractDao.java
public List<T> findAll() { if (this.entityClass == Producto.class) { entityManager.clear();/*from www.ja va 2s. c om*/ } javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); return entityManager.createQuery(cq).getResultList(); }
From source file:com.sapito.db.dao.AbstractDao.java
public List<T> findRange(int[] range) { javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); javax.persistence.Query q = entityManager.createQuery(cq); q.setMaxResults(range[1] - range[0]); q.setFirstResult(range[0]);/*from www .j a va2 s . c om*/ return q.getResultList(); }