List of usage examples for javax.persistence.criteria CriteriaBuilder like
Predicate like(Expression<String> x, String pattern);
From source file:cz.vse.webmail.EmailDAOBean.java
@Override public List<Email> getFilteredEmails(User user, EmailListFilter filter) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Email> criteria = cb.createQuery(Email.class); Root<Email> root = criteria.from(Email.class); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(root.<User>get("owner"), user)); if (StringUtils.isNoneBlank(filter.getTo())) { predicates.add(cb.like(root.<String>get("to"), "%" + filter.getTo() + "%")); }/*from ww w . j av a2 s. co m*/ criteria.select(root).where(predicates.toArray(new Predicate[] {})); return entityManager.createQuery(criteria).getResultList(); }
From source file:com.expressui.core.dao.security.query.UserQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(loginName)) { ParameterExpression<String> loginNameExp = builder.parameter(String.class, "loginName"); predicates.add(builder.like(builder.upper(user.<String>get("loginName")), loginNameExp)); }/*w ww . j a v a2s . c om*/ if (hasValue(doesNotBelongToRole)) { Subquery<User> subquery = query.subquery(User.class); Root userRole = subquery.from(UserRole.class); ParameterExpression<Role> role = builder.parameter(Role.class, "doesNotBelongToRole"); subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), role)); predicates.add(builder.not(user.in(subquery))); } return predicates; }
From source file:cc.sion.core.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/*ww w . ja va 2s . co m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // if(log.isDebugEnabled()){ // log.debug(" {}-{}-{}",names[0],filter.operator,filter.value); // } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case NE: predicates.add(builder.notEqual(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
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 ww.ja v a 2 s. co 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:com.expressui.sample.dao.query.OpportunityQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Opportunity> query, Root<Opportunity> opportunity) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(accountName)) { ParameterExpression<String> accountNameExp = builder.parameter(String.class, "accountName"); predicates.add(/*from w ww .j a va2s. co m*/ builder.like(builder.upper(opportunity.get("account").<String>get("name")), accountNameExp)); } if (hasValue(salesStages)) { ParameterExpression<Set> salesStagesExp = builder.parameter(Set.class, "salesStages"); predicates.add(builder.in(opportunity.get("salesStage")).value(salesStagesExp)); } return predicates; }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}/* w w w.ja v a 2s . c om*/ */ public List<Transaction> searchTransaction(String searchTerm) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); Predicate customerName = builder.like(root.<String>get(FIELDCONSTANT_CUSTOMERNAME), MODULO + searchTerm + MODULO); Predicate creditCardNumber = builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), MODULO + searchTerm + MODULO); Predicate description = builder.like(root.<String>get(FIELD_CONSTANT_DESCRIPTION), MODULO + searchTerm + MODULO); query.where(builder.or(customerName, creditCardNumber, description)); return super.find(query); }
From source file:com.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java
/** * Adds the filter criteria.// www.j a v a 2s . c om * * @param filters * the filters * @param query * the query */ private void addFilterCriteria(Map<String, Object> filters, CriteriaBuilder builder, Root<BulkUpload> root, CriteriaQuery query) { if (!filters.isEmpty()) { List<Predicate> predicates = new ArrayList<Predicate>(); for (Entry<String, Object> entry : filters.entrySet()) { if (CommonUtil.isNotNull(root.get(entry.getKey()))) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } query.where(predicates.toArray(new Predicate[] {})); } }
From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java
/** * Test user like./* ww w. j a va 2 s. c om*/ */ @Test public void testUserLike() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.where(builder.like(root.<String>get("lastName"), "test%")); List<User> user = entityManager.createQuery(query).getResultList(); assertEquals(10, user.size()); }
From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java
/** * Adds the filter criteria.//from w ww .j a v a2 s . c om * * @param userId * the user id * @param filters * the filters * @param builder * the builder * @param root * the root * @param query * the query */ private void addFilterCriteria(long userId, Map<String, Object> filters, CriteriaBuilder builder, Root<Statement> root, CriteriaQuery query) { List<Predicate> predicates = new ArrayList<Predicate>(); if (!filters.isEmpty()) { for (Entry<String, Object> entry : filters.entrySet()) { if (CommonUtil.isNotNull(root.get(entry.getKey()))) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } } if (CommonUtil.isNotNull(userId)) { User user = userRepository.findById(userId); if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) { Expression<String> exp = root.get(FIELD_CONSTANT_CREDIT_CARD_ID); Predicate predicate = exp.in(retrieveCreditCardIdFromUserId(userId)); predicates.add(predicate); } } query.where(predicates.toArray(new Predicate[] {})); }
From source file:org.openregistry.core.repository.jpa.JpaReferenceRepository.java
public Region getRegionByCodeOrName(final String code) { final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery<JpaRegionImpl> c = criteriaBuilder.createQuery(JpaRegionImpl.class); c.distinct(true);/* w w w . j a va 2 s.c om*/ final Root<JpaRegionImpl> region = c.from(JpaRegionImpl.class); c.where(criteriaBuilder.or(criteriaBuilder.equal(region.get(JpaRegionImpl_.code), code), criteriaBuilder.like(region.get(JpaRegionImpl_.name), code))); try { return this.entityManager.createQuery(c).getSingleResult(); } catch (final Exception e) { log.debug(e.getMessage(), e); return null; } }