List of usage examples for javax.persistence.criteria CriteriaBuilder like
Predicate like(Expression<String> x, String pattern);
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override/* ww w . ja v a2 s . c om*/ public List<ImageProductDTO> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ImageProductDTO> query = cb.createQuery(ImageProductDTO.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); 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()]))); query.select(cb.construct(ImageProductDTO.class, imageRoot.get(Image_.name), productJoin.get(Product_.name))).orderBy(cb.asc(imageRoot.get(Image_.name))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:se.kth.csc.persist.JPAStore.java
@Override public Iterable<Account> findAccounts(boolean onlyAdmin, String query) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Account> q = cb.createQuery(Account.class); Root<Account> account = q.from(Account.class); Expression<Boolean> expression = null; if (onlyAdmin) { // This looks like it could be replaced with account.get(Account_.admin), but it can't because of syntax expression = cb.equal(account.get(Account_.admin), true); }// w ww. ja va 2 s. com if (query != null) { Expression<Boolean> queryExpression = cb.like(cb.lower(account.get(Account_.name)), "%" + query.toLowerCase() + "%"); if (expression == null) { expression = queryExpression; } else { expression = cb.and(expression, queryExpression); } } if (expression != null) { q.where(expression); } return entityManager.createQuery(q.select(account)).getResultList(); }
From source file:com.jaxio.jpa.querybyexample.JpaUtil.java
public <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode, SearchParameters sp, CriteriaBuilder builder) { if (sp.isCaseInsensitive()) { path = builder.lower(path);/*from ww w . jav a2 s .c o m*/ attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale()); } switch (searchMode != null ? searchMode : sp.getSearchMode()) { case EQUALS: return builder.equal(path, attrValue); case ENDING_LIKE: return builder.like(path, "%" + attrValue); case STARTING_LIKE: return builder.like(path, attrValue + "%"); case ANYWHERE: return builder.like(path, "%" + attrValue + "%"); case LIKE: return builder.like(path, (String) attrValue); // assume user provide the wild cards default: throw new IllegalStateException("expecting a search mode!"); } }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
/** * /*ww w . ja v a2s. com*/ * @param criteriaBuilder * @param paths * @return */ protected Predicate buildMultiWordLikePredicate(CriteriaBuilder criteriaBuilder, Path<String>... paths) { String[] words = semeRicerca.split(" "); List<Predicate> likeAllFields = new ArrayList<Predicate>(); for (Path<String> path : paths) { List<Predicate> likeOnWord = new ArrayList<Predicate>(); for (String word : words) { likeOnWord.add(criteriaBuilder.like(path, getForLike(word))); } Predicate p1 = criteriaBuilder.and(likeOnWord.toArray(new Predicate[likeOnWord.size()])); likeAllFields.add(p1); likeOnWord.clear(); } Predicate res = criteriaBuilder.or(likeAllFields.toArray(new Predicate[likeAllFields.size()])); return res; }
From source file:com.dbs.sdwt.jpa.JpaUtil.java
public <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode, SearchParameters sp, CriteriaBuilder builder) { if (sp.isCaseInsensitive()) { path = builder.lower(path);/* w w w .ja va 2s . c om*/ attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale()); } switch (searchMode != null ? searchMode : sp.getSearchMode()) { case EQUALS: return builder.equal(path, attrValue); case ENDING_LIKE: return builder.like(path, "%" + attrValue); case STARTING_LIKE: return builder.like(path, attrValue + "%"); case ANYWHERE: return builder.like(path, "%" + attrValue + "%"); case LIKE: return builder.like(path, (String) attrValue); // assume user provide the wild cards case NOT_IN: return builder.notLike(path, (String) attrValue); default: throw new IllegalStateException("expecting a search mode!"); } }
From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java
@Override public Specification<Device> forOwner(final String organisation) throws ArgumentNullOrEmptyException { if (organisation == null) { throw new ArgumentNullOrEmptyException("owner"); }/*from w ww . ja v a 2s . c om*/ return new Specification<Device>() { @Override public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Subquery<Long> subquery = query.subquery(Long.class); final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class); subquery.select(deviceAuthorizationRoot.get("device").get("id").as(Long.class)); subquery.where(cb.and( cb.like(cb.upper(deviceAuthorizationRoot.get("organisation").<String>get("name")), organisation.toUpperCase()), cb.equal(deviceAuthorizationRoot.get("functionGroup"), DeviceFunctionGroup.OWNER.ordinal()))); return cb.in(deviceRoot.get("id")).value(subquery); } }; }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override// w w w . j a va2 s . c o m 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:eu.uqasar.service.ProcessService.java
private List<Predicate> getFilterPredicates(final ProcessesFilterStructure filter, CriteriaBuilder cb, Root<Process> from) { List<Predicate> predicates = new ArrayList<>(); if (filter == null) { return predicates; }// www.j av a 2 s .c om if (filter.getStartDate() != null) { predicates.add(cb.equal(from.get(Process_.startDate), filter.getStartDate())); } if (filter.getEndDate() != null) { predicates.add(cb.equal(from.get(Process_.endDate), filter.getEndDate())); } if (!StringUtils.isEmpty(filter.getName())) { Predicate firstName = cb.like(cb.lower(from.get(Process_.name)), LIKE_WILDCARD + filter.getName().toLowerCase() + LIKE_WILDCARD); predicates.add((firstName)); } return predicates; }
From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java
@Override public List<Person> findByUnknownIdentifier(final String identifierValue) throws RepositoryAccessException { final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class); final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class); final Join<JpaPersonImpl, JpaIdentifierImpl> identifier = person.join(JpaPersonImpl_.identifiers); c.select(person).distinct(true)// w w w. j a v a 2 s. co m .where(criteriaBuilder.like(identifier.get(JpaIdentifierImpl_.value), identifierValue + "%")); final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).getResultList(); return new ArrayList<Person>(persons); }
From source file:com.ims.service.ProductStockInfoService.java
public List<ProductStockInfo> findProdStockInfoListFrom(final ProdStockSearchCriteria stockSearchCriteria) { Specification<ProductStockInfo> speci = new Specification<ProductStockInfo>() { @Override//w ww. j a va2s . co 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); }