List of usage examples for javax.persistence.criteria CriteriaBuilder lower
Expression<String> lower(Expression<String> x);
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); attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale()); }// w w w . j a v a 2s . com 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:de.ks.idnadrev.information.view.InformationOverviewDS.java
private List<InformationPreviewItem> getResults(String name, List<String> tagNames, Category category, EntityManager em, CriteriaBuilder builder, Class<? extends Information<?>> clazz) { CriteriaQuery<InformationPreviewItem> query = builder.createQuery(InformationPreviewItem.class); Root<? extends Information<?>> root = query.from(clazz); ArrayList<Predicate> filters = new ArrayList<>(); if (!name.isEmpty()) { filters.add(builder.like(builder.lower(root.<String>get(KEY_NAME)), name)); }/*from w w w .j a v a2 s . c o m*/ if (!tagNames.isEmpty()) { List<Tag> tags = getTags(tagNames, em); SetJoin<TextInfo, Tag> tagJoin = root.joinSet(KEY_TAGS); filters.add(tagJoin.in(tags)); } if (category != null) { filters.add(builder.equal(root.get(KEY_CATEGORY), category)); } query.distinct(true); query.where(filters.toArray(new Predicate[filters.size()])); query.select( builder.construct(InformationPreviewItem.class, root.get(KEY_NAME), root.get(KEY_CREATIONTIME))); List<InformationPreviewItem> resultList = em.createQuery(query).getResultList(); return resultList; }
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 w w w . jav a 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:com.sapito.db.dao.AbstractDao.java
public List<T> findBySpecificField(String field, Object fieldContent, String predicates, LinkedHashMap<String, String> ordering, LinkedList<String> grouping) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); Predicate predicate = null;// w ww. j a v a 2s. c o m if (predicates.equals("equal")) { predicate = cb.equal(root.get(field), fieldContent); } else if (predicates.equals("likelower")) { predicate = cb.like(cb.lower(root.<String>get(field)), fieldContent.toString()); } else if (predicates.equals("like")) { predicate = cb.like(root.<String>get(field), "%" + fieldContent.toString() + "%"); } cq.select(root); cq.where(predicate); if (ordering != null) { Set<String> set = ordering.keySet(); List<Order> orders = new ArrayList<>(); for (String orderingField : set) { Order order; if (ordering.get(orderingField).equals("ASC")) { order = cb.asc(root.get(orderingField)); } else { order = cb.desc(root.get(orderingField)); } orders.add(order); } cq.orderBy(orders); } if (grouping != null) { Iterator iterator = grouping.iterator(); List<Expression> groups = new LinkedList<>(); while (iterator.hasNext()) { groups.add(root.get(iterator.next().toString())); } cq.groupBy(groups); } Query query = entityManager.createQuery(cq); query.setMaxResults(MAX_RECORDS_RETURNED); return query.getResultList(); }
From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java
private Predicate addFilters(Predicate p, CriteriaBuilder cb, String hostFilter, String appServerFilter, String runtimeFilter, String nodeFilter, String contextFilter, Path<String> hostPath, Path<String> appServerPath, Path<String> runtimePath, Path<String> nodePath, Path<String> contextPath) { if (!StringUtils.isEmpty(hostFilter)) { p = cb.and(p,//from w w w .j av a 2 s . c o m cb.like(cb.lower(hostPath), hostFilter.toLowerCase(), JpaWildcardConverter.ESCAPE_CHARACTER)); } if (!StringUtils.isEmpty(nodeFilter)) { p = cb.and(p, cb.like(cb.lower(nodePath), nodeFilter.toLowerCase(), JpaWildcardConverter.ESCAPE_CHARACTER)); } if (!StringUtils.isEmpty(appServerFilter)) { p = cb.and(p, cb.like(cb.lower(appServerPath), appServerFilter.toLowerCase(), JpaWildcardConverter.ESCAPE_CHARACTER)); } if (!StringUtils.isEmpty(runtimeFilter)) { p = cb.and(p, cb.like(cb.lower(runtimePath), runtimeFilter.toLowerCase(), JpaWildcardConverter.ESCAPE_CHARACTER)); } if (!StringUtils.isEmpty(contextFilter)) { p = cb.and(p, cb.equal(cb.lower(contextPath), contextFilter.toLowerCase())); } p = cb.and(p, cb.notEqual(contextPath, LOCAL_ENV)); return p; }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercilessly() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/* www . ja va 2s . co m*/ public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> product = query.from(Product.class); query.select(product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%")); Subquery<Long> subQuery = query.subquery(Long.class); Root<Image> infoRoot = subQuery.from(Image.class); Join<Image, Product> productJoin = infoRoot.join(Image_.product); subQuery.select(productJoin.<Long>get(Product_.id)); subQuery.where(cb.gt(infoRoot.get(Image_.index), 0)); criteria.add(cb.in(product.get(Product_.id)).value(subQuery)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:eu.uqasar.service.ProductService.java
private List<Predicate> getFilterPredicates(final ProductFilterStructure filter, CriteriaBuilder cb, Root<Product> from) {/*from w w w . j a v a2 s . c om*/ List<Predicate> predicates = new ArrayList<>(); if (filter == null) { return predicates; } if (filter.getReleaseDate() != null) { predicates.add(cb.equal(from.get(Product_.releaseDate), filter.getReleaseDate())); } if (filter.getVersion() != null) { predicates.add(cb.equal(from.get(Product_.version), filter.getVersion())); } if (!StringUtils.isEmpty(filter.getName())) { Predicate firstName = cb.like(cb.lower(from.get(Product_.name)), LIKE_WILDCARD + filter.getName().toLowerCase() + LIKE_WILDCARD); predicates.add(firstName); } return predicates; }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override//from w w w . j a v a2 s . co m 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); }//from ww w .ja v a 2s . co m 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.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/*from w w w . j a v a2 s. co 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(); } }); }