List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:com.ims.service.ProductStockInfoService.java
public List<ProductStockInfo> findProdStockInfoListFrom(final ProdStockSearchCriteria stockSearchCriteria) { Specification<ProductStockInfo> speci = new Specification<ProductStockInfo>() { @Override//from w ww . j a v a 2 s . c o m public Predicate toPredicate(Root<ProductStockInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(root.get("stockType"), stockSearchCriteria.getStockType())); if (StringUtils.isNotEmpty(stockSearchCriteria.getProdCategoryCode())) { predicates.add(cb.equal(root.get("categoryCode"), stockSearchCriteria.getProdCategoryCode())); } if (StringUtils.isNotEmpty(stockSearchCriteria.getProdCode())) { predicates.add(cb.like(root.<String>get("productCode"), "%" + stockSearchCriteria.getProdCode() + "%")); } String compareCode = stockSearchCriteria.getCompareCode(); if (StringUtils.isNotEmpty(compareCode) && stockSearchCriteria.isIncludeComparedValue()) { Path<ProductAmount> productAmount = root.<ProductAmount>get("productAmount"); if (CompareCode.isEqual(compareCode)) { predicates.add( cb.equal(productAmount.get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isGreater(compareCode)) { predicates.add(cb.greaterThan(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isLess(compareCode)) { predicates.add(cb.lessThan(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isGreaterOrEqual(compareCode)) { predicates.add(cb.greaterThanOrEqualTo(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isLessOrEqual(compareCode)) { predicates.add(cb.lessThanOrEqualTo(productAmount.<Integer>get("totalAmount"), stockSearchCriteria.getStockAmount())); } else if (CompareCode.isEqualAlertAmount(compareCode)) { predicates.add(cb.equal(productAmount.get("totalAmount"), root.get("alertStockAmount"))); } else if (CompareCode.isGreaterThanAlertAmount(compareCode)) { predicates.add(cb.greaterThan(productAmount.<String>get("totalAmount"), root.<String>get("alertStockAmount"))); } else if (CompareCode.isLessThanAlertAmount(compareCode)) { predicates.add(cb.lessThan(productAmount.<String>get("totalAmount"), root.<String>get("alertStockAmount"))); } if (stockSearchCriteria.isTransformAction() && ((CompareCode.isLess(compareCode) || CompareCode.isLessOrEqual(compareCode)))) { predicates.add(cb.greaterThan(productAmount.<Integer>get("totalAmount"), 0)); } } query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))) .orderBy(cb.desc(root.get("categoryCode")), cb.asc(root.get("productCode"))); return null; } }; return productStockInfoRepository.findAll(speci); }
From source file:eu.uqasar.service.user.UserService.java
public User getByFullName(final String fullName) { logger.infof("loading User with fullName %s ...", fullName); User entity = null;/*from www . j a va 2 s.c o m*/ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> root = query.from(User.class); query.where(cb.equal(cb.lower(cb.concat(root.get(User_.firstName), root.get(User_.lastName))), fullName.toLowerCase())); List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList(); if (!resultList.isEmpty()) { return resultList.get(0); } return entity; }
From source file:utils.jpa.EntityResource.java
private List<T> getAllPriv(@BeanParam TableSearchQuery tb, List<String> fieldsSearchBy) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root);/*www.j av a 2s .c om*/ //setting order if (tb.getOrderType().equals(OrderType.DESC)) { cq.orderBy(cb.desc(root.get(tb.getOrder()))); } else { cq.orderBy(cb.asc(root.get(tb.getOrder()))); } Query query = tb.createQuery(em, cb, cq, root); if (query == null) { query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy); } if (tb.getOffset() > 0) { query.setFirstResult(tb.getOffset()); } if (tb.getLimit() > 0) { query.setMaxResults(tb.getLimit()); } return query.getResultList(); }
From source file:com.wms.studio.service.WallpaperServiveImpl.java
@Override @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<WallpaperDto> findBy(final WallpaperEnum wallpaperType, final Date startDate, final Date endDate, PageSize pageSize) {/*from w w w .j a va2s . c o m*/ if (pageSize == null) { pageSize = new PageSize(); } Page<Wallpaper> pageWallpaper = this.wallpaperRepository.findAll(new Specification<Wallpaper>() { @Override public Predicate toPredicate(Root<Wallpaper> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<Predicate>(); if (wallpaperType != null) { pres.add(cb.equal(root.get("wallpaperType").as(WallpaperEnum.class), wallpaperType)); } if (startDate != null) { pres.add(cb.greaterThanOrEqualTo(root.get("addDate").as(Date.class), startDate)); } if (endDate != null) { pres.add(cb.lessThanOrEqualTo(root.get("addDate").as(Date.class), endDate)); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit())); return this.wallpaperCovert.covertToDto(pageWallpaper); }
From source file:edu.sabanciuniv.sentilab.sare.models.base.documentStore.PersistentDocumentStore.java
/** * Gets identifiers of documents in this store. * @param em the {@link EntityManager} to get the documents from. * @return an {@link Iterable} of document identifiers. *//* w w w . ja va 2 s. com*/ public Iterable<byte[]> getDocumentIds(EntityManager em) { Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em"); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class); Root<PersistentDocument> doc = cq.from(PersistentDocument.class); cq.multiselect(doc.get("id")) .where(cb.equal(doc.get("store"), cb.parameter(PersistentDocumentStore.class, "store"))); TypedQuery<byte[]> tq = em.createQuery(cq); tq.setParameter("store", this); return tq.getResultList(); }
From source file:eu.uqasar.service.user.UserService.java
public User getByFullNameWithUsername(final String fullNameWithUsername) { logger.infof("loading User with fullName and username %s ...", fullNameWithUsername); User entity = null;/*from w w w .ja va2 s . c o m*/ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> root = query.from(User.class); Expression<String> fullName = cb.concat(cb.concat(root.get(User_.firstName), " "), root.get(User_.lastName)); Expression<String> userName = cb.concat(" (", cb.concat(root.get(User_.userName), ")")); query.where(cb.equal(cb.lower(cb.concat(fullName, userName)), fullNameWithUsername.toLowerCase())); List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList(); if (!resultList.isEmpty()) { return resultList.get(0); } return entity; }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e FROM jpa_query_employee e WHERE e.name='employee_1' *///w w w. ja v a2s . c om @Transactional public void doSimple2() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); c.select(e).where(cb.equal(e.get("name"), "employee_1")); showResult(c); }
From source file:eu.uqasar.service.ProductService.java
public List<Product> getAllByAscendingNameFiltered( eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> from = criteria.from(Product.class); List<Predicate> predicates = getFilterPredicates(filter, cb, from); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//from ww w . j a v a 2s. co m criteria.orderBy(cb.asc(from.get(Product_.releaseDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.ProductService.java
public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> root = criteria.from(Product.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }/*from w w w . j a v a 2 s . c om*/ criteria.orderBy(cb.desc(root.get(Product_.releaseDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java
@Override public List<PurchaseOrder> findAll() { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); javax.persistence.criteria.CriteriaQuery cq = cb.createQuery(); Root<PurchaseOrder> root = cq.from(PurchaseOrder.class); cq.select(root);//from www . j av a 2 s.c om ParameterExpression<String> status = cb.parameter(String.class); // cq.where(cb.notEqual(invRoot.get("status"), status)); cq.where(cb.notEqual(root.get("deleted"), 'Y')); cq.orderBy(cb.desc(root.get("purchaseOrderId"))); Query query = getEntityManager().createQuery(cq); // query.setParameter(status, PaymentStatusEnum.DELETE.getValue()); List<PurchaseOrder> list = query.getResultList(); for (PurchaseOrder wo : list) { List<PurchaseOrderItems> tempDel = new ArrayList<>(); for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) { if (wot.isDeleted()) { tempDel.add(wot); } } wo.getPurchaseOrderItems().removeAll(tempDel); } return list; }