List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:net.groupbuy.dao.impl.MessageDaoImpl.java
public Page<Message> findPage(Member member, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class); Root<Message> root = criteriaQuery.from(Message.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forMessage")), criteriaBuilder.equal(root.get("isDraft"), false)); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(//from w w w . ja va 2 s . c om criteriaBuilder.and(criteriaBuilder.equal(root.get("sender"), member), criteriaBuilder.equal(root.get("senderDelete"), false)), criteriaBuilder.and(criteriaBuilder.equal(root.get("receiver"), member), criteriaBuilder.equal(root.get("receiverDelete"), false)))); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or( criteriaBuilder.and(criteriaBuilder.isNull(root.get("sender")), criteriaBuilder.equal(root.get("senderDelete"), false)), criteriaBuilder.and(criteriaBuilder.isNull(root.get("receiver")), criteriaBuilder.equal(root.get("receiverDelete"), false)))); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:com.netflix.genie.server.metrics.impl.JobCountManagerImpl.java
/** * {@inheritDoc}/*from www. j a v a 2 s .c om*/ */ @Override @Transactional(readOnly = true) //TODO: Move to specification public int getNumInstanceJobs(final String hostName, final Long minStartTime, final Long maxStartTime) throws GenieException { LOG.debug("called"); final String finalHostName; // initialize host name if (StringUtils.isBlank(hostName)) { finalHostName = NetUtil.getHostName(); } else { finalHostName = hostName; } final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<Long> cq = cb.createQuery(Long.class); final Root<Job> j = cq.from(Job.class); cq.select(cb.count(j)); final Predicate runningStatus = cb.equal(j.get(Job_.status), JobStatus.RUNNING); final Predicate initStatus = cb.equal(j.get(Job_.status), JobStatus.INIT); final List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.equal(j.get(Job_.hostName), finalHostName)); predicates.add(cb.or(runningStatus, initStatus)); if (minStartTime != null) { predicates.add(cb.greaterThanOrEqualTo(j.get(Job_.started), new Date(minStartTime))); } if (maxStartTime != null) { predicates.add(cb.lessThan(j.get(Job_.started), new Date(maxStartTime))); } //documentation says that by default predicate array is conjuncted together cq.where(predicates.toArray(new Predicate[predicates.size()])); final TypedQuery<Long> query = this.em.createQuery(cq); //Downgrading to an int since all the code seems to want ints //Don't feel like changing everthing right now can figure out later //if need be return query.getSingleResult().intValue(); }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, OrderSearchParams params, Set<UUID> processingPeriodIds, Pageable pageable, boolean count, Set<UUID> availableSupplyingFacilities, Set<UUID> availableRequestingFacilities) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); Root<Order> root = query.from(Order.class); if (count) {/*ww w. j av a 2 s . co m*/ CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query; query = (CriteriaQuery<T>) countQuery.select(builder.count(root)); } Predicate predicate = builder.conjunction(); predicate = isEqual(SUPPLYING_FACILITY_ID, params.getSupplyingFacilityId(), root, predicate, builder); predicate = isEqual(REQUESTING_FACILITY_ID, params.getRequestingFacilityId(), root, predicate, builder); if (!(isEmpty(availableSupplyingFacilities) && isEmpty(availableRequestingFacilities))) { Predicate orPredicate = builder.disjunction(); orPredicate = isOneOfOr(SUPPLYING_FACILITY_ID, availableSupplyingFacilities, root, orPredicate, builder); orPredicate = isOneOfOr(REQUESTING_FACILITY_ID, availableRequestingFacilities, root, orPredicate, builder); predicate = builder.and(predicate, orPredicate); } predicate = isEqual(PROGRAM_ID, params.getProgramId(), root, predicate, builder); predicate = isOneOf(PROCESSING_PERIOD_ID, processingPeriodIds, root, predicate, builder); predicate = isOneOf(ORDER_STATUS, params.getStatusAsEnum(), root, predicate, builder); query.where(predicate); if (!count && pageable != null && pageable.getSort() != null) { query = addSortProperties(query, root, pageable); } return query; }
From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java
/** * {@inheritDoc}/* w w w . j ava2 s . c o m*/ */ public long getStatementsCount(Map<String, Object> filters, long userId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<Statement> root = query.from(Statement.class); query.select(builder.count(root)); addFilterCriteria(userId, filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:com.greendot.db.jpa.core.AbstractSearchDao.java
@Transactional(readOnly = true) public List<E> findAll() { try {/* w w w . ja v a 2 s . c o m*/ final CriteriaQuery<E> query = getEntityManager().getCriteriaBuilder().createQuery(entityType); final Root<E> root = query.from(entityType); final CriteriaQuery<E> rootQuery = query.select(root); final TypedQuery<E> allQuery = getEntityManager().createQuery(rootQuery); stopWatchThread.get().start(); final List<E> rows = allQuery.getResultList(); stopWatchThread.get().stop(); LOG.info("Located: [{} rows] during findAll({}) in [{}ms]", rows.size(), entityType.getSimpleName(), stopWatchThread.get().getLastTaskTimeMillis()); return rows; } finally { if (stopWatchThread.get().isRunning()) stopWatchThread.get().stop(); } }
From source file:net.groupbuy.dao.impl.CouponCodeDaoImpl.java
public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class); Root<CouponCode> root = criteriaQuery.from(CouponCode.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); }//w w w . j a v a2 s . co m if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date())); } } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(), criteriaBuilder.lessThan(couponPath.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("endDate").isNull(), criteriaBuilder.greaterThanOrEqualTo(couponPath.<Date>get("endDate"), new Date()))); } } if (isUsed != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
/** * Retrieves a single {@link ResourceEntity} by the given attribute and value. * /* www . j av a2 s .c o m*/ * @param attribute * The attribute of the resource entity to retrieve it by * @param value * The value of the attribute to compare it to * @param clazz * The concrete resource entity class to retrieve (may also be {@link ResourceEntity}) * @return The matching {@link ResourceEntity} * @throws ResourceNotFoundException * If no {@link ResourceEntity} could be found * @throws OsiamException * If more than 1 {@link ResourceEntity} was found */ public <T extends ResourceEntity, V> T getByAttribute(SingularAttribute<? super T, V> attribute, V value, Class<T> clazz) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(clazz); Root<T> resource = cq.from(clazz); cq.select(resource).where(cb.equal(resource.get(attribute), value)); TypedQuery<T> q = em.createQuery(cq); try { return q.getSingleResult(); } catch (NoResultException nre) { throw new ResourceNotFoundException( String.format("Resource with attribute '%s' set to '%s' not found", attribute.getName(), value), nre); } catch (NonUniqueResultException nure) { throw new OsiamException(String.format("Muliple resources with attribute '%s' set to '%s' found", attribute.getName(), value), nure); } }
From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java
public List<StaticAsset> readAllStaticAssets() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler); List<Predicate> restrictions = new ArrayList<Predicate>(); List<Order> sorts = new ArrayList<Order>(); try {/*from w w w . java2 s . co m*/ if (queryExtensionManager != null) { queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null); queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions); queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria, handler, sorts); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); return em.createQuery(criteria).getResultList(); } catch (NoResultException e) { return new ArrayList<StaticAsset>(); } }
From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java
@Override public Collection<Board> boardListWithPaging(Board vo) { int bbsId = vo.getBbsId(); int pageSize = vo.getPageSize(); int pageNumber = (int) vo.getPageIndex(); CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Board> criteriaQuery = criteriaBuilder.createQuery(Board.class); Root<Board> from = criteriaQuery.from(Board.class); CriteriaQuery<Board> select = criteriaQuery.select(from); if (bbsId > 0) { criteriaQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId)); }//from w w w . j a v a 2 s .c o m /**list desc for date*/ criteriaQuery.orderBy(criteriaBuilder.desc(from.get("frstRegistPnttm"))); TypedQuery<Board> typedQuery = em.createQuery(select); typedQuery.setFirstResult((pageNumber - 1) * pageSize); typedQuery.setMaxResults(pageSize); Collection<Board> fooList = typedQuery.getResultList(); return fooList; }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
public <T extends ResourceEntity, V> boolean isUniqueAttributeAlreadyTaken(String attributeValue, String id, SingularAttribute<? super T, V> attribute, Class<T> clazz) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<T> resource = cq.from(clazz); cq.select(cb.countDistinct(resource)); Predicate predicate = cb.equal(resource.get(attribute), attributeValue); if (id != null) { Predicate ignoreId = cb.notEqual(resource.get(ResourceEntity_.id), id); predicate = cb.and(predicate, ignoreId); }/*from w ww .j a va2 s . co m*/ cq.where(predicate); TypedQuery<Long> countQuery = em.createQuery(cq); return countQuery.getSingleResult() > 0; }