List of usage examples for javax.persistence.criteria CriteriaBuilder like
Predicate like(Expression<String> x, String pattern);
From source file:com.hengyi.japp.execution.Util.java
public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Task> root, TaskQueryCommand command) {//w w w.j a va2 s .c o m Predicate p1 = cb.equal(root.get(Task_.charger), command.getOperator()); ListJoin<Task, Operator> joinFollowers = root.join(Task_.followers, JoinType.LEFT); Predicate p2 = cb.equal(joinFollowers.get(Operator_.id), command.getOperator().getId()); ListJoin<Task, Operator> joinExecutors = root.join(Task_.executors, JoinType.LEFT); Predicate p3 = cb.equal(joinExecutors.get(Operator_.id), command.getOperator().getId()); Predicate p = cb.or(p1, p2, p3); if (command.getExecutor() != null) { p = cb.and(p, cb.equal(p, cb.isMember(command.getExecutor(), root.get(Task_.executors)))); } if (command.getCustomer() != null) { p = cb.and(p, cb.equal(root.get(Task_.customer), command.getCustomer())); } if (!isBlank(command.getContent())) { p = cb.and(p, cb.like(root.get(Task_.content), command.getContentQuery())); } Collection<TaskType> TaskTypes = command.getTypes(); if (TaskTypes != null && !TaskTypes.isEmpty()) { p = cb.and(p, root.get(Task_.type).in(TaskTypes)); } Collection<TaskStatus> statuses = command.getStatuses(); if (statuses != null && !statuses.isEmpty()) { p = cb.and(p, root.get(Task_.status).in(statuses)); } if (command.getCreateDate() != null) { Date createDateStart = LocalDate.fromDateFields(command.getCreateDate()).toDate(); Date createDateEnd = LocalDate.fromDateFields(command.getCreateDate()).plusDays(1).toDate(); p = cb.and(p, cb.between(root.get(Task_.logInfo).get(LogInfo_.createDateTime), createDateStart, createDateEnd)); // TODO timestamp date convert //p = cb.and(p, cb.equal(root.get(Task_.logInfo).get(LogInfo_.createDateTime).as(java.sql.Date.class), command.getCreateDate())); } cq.where(p); }
From source file:com.mxep.web.common.persistence.DynamicSpecifications.java
private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) { 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]); }/*w w w .j a va 2s. c o m*/ /*????*/ if (expression.getJavaType().equals(Integer.class) && !filter.operator.equals(SearchFilter.Operator.IN)) { filter.value = Integer.parseInt(filter.value.toString()); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(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; case NEQ: predicates.add(builder.notEqual(expression, filter.value)); break; case IN: In in = builder.in(expression); String value = filter.value.toString(); String[] values = StringUtils.split(value, ","); for (int i = 0; i < values.length; i++) { /*????*/ if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class) || expression.getJavaType().equals(byte.class)) { in.value(Integer.valueOf(values[i])); } else { in.value(values[i]); } } predicates.add(in); break; case NULL: predicates.add(builder.isNull(expression)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; } } return predicates; }
From source file:com.juliuskrah.multipart.repository.impl.HibernateAccountRepository.java
@Override public Stream<Account> findByLastName(String lastName) { Session session = this.em.unwrap(Session.class); CriteriaBuilder qb = session.getCriteriaBuilder(); CriteriaQuery<Account> cq = qb.createQuery(Account.class); Root<Account> root = cq.from(Account.class); Predicate predicate = qb.like(root.get(Account_.lastName), lastName); cq.where(predicate);// w w w . j av a2 s . co m return session.createQuery(cq).stream(); }
From source file:com.juliuskrah.multipart.repository.impl.HibernateAccountRepository.java
@Override public Optional<Account> findByUsername(String username) { CriteriaBuilder qb = this.em.getCriteriaBuilder(); CriteriaQuery<Account> query = qb.createQuery(Account.class); Root<Account> root = query.from(Account.class); Predicate predicate = qb.like(root.get(Account_.username), username); query.where(predicate);// w w w . j ava2 s . c o m Query q = this.em.createQuery(query); return Optional.of((Account) q.getSingleResult()); }
From source file:com.globalhackv.app.repository.CitationSpec.java
@Override public Predicate toPredicate(Root<Citation> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<Predicate>(); if (StringUtils.isNotBlank(example.getLastName())) { predicates.add(/*w w w . j a va 2 s . c o m*/ cb.like(cb.lower(root.get(Citation_.lastName)), example.getLastName().toLowerCase() + "%")); } if (StringUtils.isNotBlank(example.getFirstName())) { predicates.add( cb.like(cb.lower(root.get(Citation_.firstName)), example.getFirstName().toLowerCase() + "%")); } if (StringUtils.isNotBlank(example.getDateOfBirth())) { predicates.add(cb.like(cb.lower(root.get(Citation_.dateOfBirth)), example.getDateOfBirth().toLowerCase() + "%")); } if (StringUtils.isNotBlank(example.getDriversLicense())) { predicates.add(cb.like(cb.lower(root.get(Citation_.driversLicense)), example.getDriversLicense().toLowerCase() + "%")); } if (StringUtils.isNotBlank(example.getAddress())) { predicates .add(cb.like(cb.lower(root.get(Citation_.address)), example.getAddress().toLowerCase() + "%")); } if (example.getCitationNumber() != 0) { predicates.add(cb.equal(root.get(Citation_.citationNumber), example.getCitationNumber())); } return andTogether(predicates, cb); }
From source file:core.commonapp.server.dao.security.UserLoginDaoHibernateImpl.java
@Override public List<UserLogin> findAllLikeUsername(String username) { CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<UserLogin> query = builder.createQuery(UserLogin.class); Root<UserLogin> root = query.from(UserLogin.class); query.where(builder.like(root.<String>get("username"), "%" + username + "%")); return getEntityManager().createQuery(query).getResultList(); }
From source file:cn.imethan.common.repository.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/*ww w. j a v a 2 s . c o m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (filters != null && !filters.isEmpty()) { 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]); try { for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } } catch (Exception e) { e.printStackTrace(); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(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; case IN: In in = builder.in(expression); String[] valueStrings = StringUtils.split(filter.value.toString(), ","); List<Long> list = new ArrayList<Long>(); for (String value : valueStrings) { list.add(Long.valueOf(value.trim())); } in.value(list); predicates.add(in); break; case NOTIN: In in1 = builder.in(expression); String[] valueStrings1 = StringUtils.split(filter.value.toString(), ","); List<Long> list1 = new ArrayList<Long>(); for (String value : valueStrings1) { list1.add(Long.valueOf(value.trim())); } in1.value(list1); predicates.add(builder.not(in1)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } // //hibernate // //org.hibernate.ejb.criteria.CriteriaQueryImpl // if(query instanceof org.hibernate.ejb.QueryImpl) { // @SuppressWarnings("rawtypes") // org.hibernate.ejb.QueryImpl hibernateQuery = (org.hibernate.ejb.QueryImpl)query; // org.hibernate.Query hQuery = hibernateQuery.getHibernateQuery(); // hQuery.setCacheable(true); // } return builder.conjunction(); } }; }
From source file:de.logicline.splash.dao.ContactDaoImpl.java
public List<ContactEntity> findByName(String name) { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<ContactEntity> cq = cb.createQuery(ContactEntity.class); Root<ContactEntity> userInfo = cq.from(ContactEntity.class); cq.select(userInfo);// ww w . j av a2 s . c o m cq.where(cb.like(userInfo.<String>get("lastName"), "%" + name + "%")); List<ContactEntity> result = getEntityManager().createQuery(cq).getResultList(); return result; }
From source file:org.zlogic.vogon.web.data.TransactionFilterSpecification.java
/** * Builds the Predicate/*from ww w . j ava2 s .co m*/ * * @param root the FinanceTransaction query root * @param cq the CriteriaQuery instance * @param cb the CriteriaBuilder instance * @return the Predicate of this filter */ @Override public Predicate toPredicate(Root<FinanceTransaction> root, CriteriaQuery<?> cq, CriteriaBuilder cb) { Predicate ownerPredicate = cb.equal(root.get(FinanceTransaction_.owner), owner); Predicate descriptionPredicate = filterDescription != null ? cb.like(cb.lower(root.get(FinanceTransaction_.description)), filterDescription.toLowerCase()) : cb.conjunction(); Predicate tagsPredicate = cb.conjunction(); if (filterTags != null && !filterTags.isEmpty()) { Set<String> filterTagsLowercase = new HashSet<>(); for (String tag : filterTags) filterTagsLowercase.add(tag.toLowerCase()); tagsPredicate = cb.lower(root.join(FinanceTransaction_.tags)).in(cb.literal(filterTagsLowercase)); } Predicate datePredicate = filterDate != null ? cb.equal(root.get(FinanceTransaction_.transactionDate), new java.sql.Date(filterDate.getTime())) : cb.conjunction(); return cb.and(ownerPredicate, descriptionPredicate, datePredicate, tagsPredicate); }
From source file:com.expressui.sample.view.role.RoleQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Role> rootEntity) { List<Predicate> criteria = new ArrayList<Predicate>(); if (!isEmpty(name)) { ParameterExpression<String> p = builder.parameter(String.class, "name"); criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p)); }/*from w w w .j av a 2 s. co m*/ if (!isEmpty(doesNotBelongToUser)) { ParameterExpression<User> p = builder.parameter(User.class, "doesNotBelongToUser"); Join join = rootEntity.join("userRoles", JoinType.LEFT); criteria.add(builder.or(builder.notEqual(join.get("user"), p), builder.isNull(join.get("user")))); } return criteria; }