List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:com.fantasy.stataggregator.workers.GameDataRetrieverTask.java
/** * Sets the statistical year to be requested. * * @param year//from ww w . j a v a 2 s . com * @throws java.text.ParseException */ @Override public void setYear(int year) throws ParseException { if (Objects.nonNull(ctx)) { this.year = year; isTaskComplete = false; GameScheduleRepository gsr = ctx.getBean(GameScheduleRepository.class); if (year == Integer.MAX_VALUE) { schedules = gsr.findAll(); } else { SimpleDateFormat sdf = ctx.getBean(SimpleDateFormat.class); sdf.applyLocalizedPattern("yyyyMMdd"); Date min = sdf.parse(year + START_OF_YEAR); Date max = sdf.parse(year + END_OF_YEAR); CriteriaBuilder cb = gsr.getCriteriaBuilder(); CriteriaQuery<GameSchedule> cq = gsr.getCriteriaQuery(); Root<GameSchedule> gameSchedule = gsr.getRoot(); cq.select(gameSchedule).where(cb.between(gameSchedule.get(GameSchedule_.gamedate), min, max)) .orderBy(cb.asc(gameSchedule.get(GameSchedule_.gameid))); schedules = gsr.getCriteriaList(cq); System.out.println(schedules.size()); } } }
From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java
/** * Gets the quantity./*from w w w . ja v a 2 s.com*/ * * @param dbAccess * the db access * @param target * the target * @return the quantity * @since 28/08/2014, 11:20:27 PM */ public int getQuantity(EntityManager dbAccess, Class<Entity> target) { CriteriaQuery<Long> cq = dbAccess.getCriteriaBuilder().createQuery(Long.class); Root<Entity> rt = cq.from(target); cq.select(dbAccess.getCriteriaBuilder().count(rt)); Query q = dbAccess.createQuery(cq); return ((Long) q.getSingleResult()).intValue(); }
From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java
@Override public StaticAsset readStaticAssetByFullUrl(String fullUrl) { 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>(); restrictions.add(builder.equal(handler.get("fullUrl"), fullUrl)); try {//w w w. ja v a2s. 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()])); if (!org.apache.commons.collections.CollectionUtils.isEmpty(sorts)) { criteria.orderBy(sorts); } TypedQuery<StaticAsset> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); List<StaticAsset> response = query.getResultList(); if (response.size() > 0) { return response.get(0); } return null; } finally { if (queryExtensionManager != null) { queryExtensionManager.getProxy().breakdown(StaticAssetImpl.class, null); } } }
From source file:de.tudarmstadt.ukp.csniper.webapp.security.dao.AbstractDao.java
/** * Finds all entities that have the same type as the given example and all fields are equal to * non-null fields in the example./*from w ww . ja v a 2 s . c o m*/ */ protected <TT> CriteriaQuery<TT> queryByExample(TT aExample, String aOrderBy, boolean aAscending) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); @SuppressWarnings("unchecked") CriteriaQuery<TT> query = cb.createQuery((Class<TT>) aExample.getClass()); @SuppressWarnings("unchecked") Root<TT> root = query.from((Class<TT>) aExample.getClass()); query.select(root); List<Predicate> predicates = new ArrayList<Predicate>(); BeanWrapper a = PropertyAccessorFactory.forBeanPropertyAccess(aExample); // Iterate over all properties for (PropertyDescriptor d : a.getPropertyDescriptors()) { Object value = a.getPropertyValue(d.getName()); // Only consider writeable properties. This filters out e.g. the "class" (getClass()) // property. if (value != null && a.isWritableProperty(d.getName())) { predicates.add(cb.equal(root.get(d.getName()), value)); } } if (!predicates.isEmpty()) { query.where(predicates.toArray(new Predicate[predicates.size()])); } if (aOrderBy != null) { if (aAscending) { query.orderBy(cb.asc(root.get(aOrderBy))); } else { query.orderBy(cb.desc(root.get(aOrderBy))); } } return query; }
From source file:utils.jpa.EntityResource.java
public int getAllCount(TableSearchQuery tb, List<String> fieldsSearchBy) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); cq.select(cb.count(root)); Query query = tb.createQuery(em, cb, cq, root); if (query == null) { query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy); }/* w w w .j a va 2 s . c om*/ try { return ((Long) query.getSingleResult()).intValue(); } catch (NoResultException ex) { return 0; } }
From source file:eu.uqasar.service.ProductService.java
/** * /* www .j a va2 s. com*/ * @param productId * @return */ public boolean productExists(Long productId) { logger.info(String.format("checking if product with ID %d exists ...", productId)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Product> from = criteria.from(Product.class); criteria.where(cb.equal(from.get(Product_.id), productId)); criteria.select(cb.countDistinct(from)); return (em.createQuery(criteria).getSingleResult() == 1); }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}//ww w. j a v a 2s . c o m */ public long getTransactionsCount(Map<String, Object> filters, long userId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<Transaction> root = query.from(Transaction.class); query.select(builder.count(root)); addFilterCriteria(userId, filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:org.finra.herd.dao.impl.BaseJpaDaoImpl.java
@Override public Timestamp getCurrentTimestamp() { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Timestamp> criteria = builder.createQuery(Timestamp.class); // Add the clauses for the query. criteria.select(builder.currentTimestamp()).from(ConfigurationEntity.class); return entityManager.createQuery(criteria).setMaxResults(1).getSingleResult(); }
From source file:net.shopxx.dao.impl.StatisticDaoImpl.java
public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) { Assert.notNull(period);/*from ww w.j av a2 s. c o m*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class); Root<Statistic> root = criteriaQuery.from(Statistic.class); switch (period) { case year: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year")); break; case month: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year"), root.get("month")); break; case day: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"), root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"), root.<BigDecimal>get("completeOrderAmount"))); break; } Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { Calendar calendar = DateUtils.toCalendar(beginDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.greaterThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day)))); } if (endDate != null) { Calendar calendar = DateUtils.toCalendar(endDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.lessThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day)))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
private <T extends ResourceEntity> long getTotalResults(Class<T> clazz, Subquery<Long> internalIdQuery) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> resourceQuery = cb.createQuery(Long.class); Root<T> resourceRoot = resourceQuery.from(clazz); resourceQuery.select(cb.count(resourceRoot)) .where(cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery)); Long total = em.createQuery(resourceQuery).getSingleResult(); return total; }