Java Code Examples for javax.persistence.criteria.CriteriaBuilder#like()
The following examples show how to use
javax.persistence.criteria.CriteriaBuilder#like() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateIdentifiableObjectStore.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 8 votes |
@Override public List<T> getAllLikeName( String name, boolean caseSensitive ) { CriteriaBuilder builder = getCriteriaBuilder(); Function<Root<T>, Predicate> likePredicate; if ( caseSensitive ) { likePredicate = root -> builder.like( root.get( "name" ), "%" + name + "%" ); } else { likePredicate = root -> builder.like( builder.lower( root.get( "name" ) ), "%" + name.toLowerCase() + "%" ); } JpaQueryParameters<T> param = new JpaQueryParameters<T>() .addPredicates( getSharingPredicates( builder ) ) .addPredicate( likePredicate ) .addOrder( root -> builder.asc( root.get( "name" ) ) ); return getList( builder, param ); }
Example 2
Source File: HibernateIdentifiableObjectStore.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public List<T> getAllLikeName( String name, int first, int max, boolean caseSensitive ) { CriteriaBuilder builder = getCriteriaBuilder(); Function<Root<T>, Predicate> likePredicate; if ( caseSensitive ) { likePredicate = root -> builder.like( root.get( "name" ), "%" + name + "%" ); } else { likePredicate = root -> builder.like( builder.lower( root.get( "name" ) ), "%" + name.toLowerCase() + "%" ); } JpaQueryParameters<T> param = new JpaQueryParameters<T>() .addPredicates( getSharingPredicates( builder ) ) .addPredicate( likePredicate ) .addOrder( root -> builder.asc( root.get( "name" ) ) ) .setFirstResult( first ) .setMaxResults( max ); return getList( builder, param ); }
Example 3
Source File: UserNamespaceAuthorizationDaoImpl.java From herd with Apache License 2.0 | 6 votes |
@Override public List<UserNamespaceAuthorizationEntity> getUserNamespaceAuthorizationsByUserIdStartsWith(String userIdStartsWith) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<UserNamespaceAuthorizationEntity> criteria = builder.createQuery(UserNamespaceAuthorizationEntity.class); // The criteria root is the user namespace authorization. Root<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntity = criteria.from(UserNamespaceAuthorizationEntity.class); // Join to the other tables we can filter on. Join<UserNamespaceAuthorizationEntity, NamespaceEntity> namespaceEntity = userNamespaceAuthorizationEntity.join(UserNamespaceAuthorizationEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.like(builder.upper(userNamespaceAuthorizationEntity.get(UserNamespaceAuthorizationEntity_.userId)), userIdStartsWith.toUpperCase() + '%'); // Add all clauses for the query. criteria.select(userNamespaceAuthorizationEntity).where(queryRestriction) .orderBy(builder.asc(userNamespaceAuthorizationEntity.get(UserNamespaceAuthorizationEntity_.userId)), builder.asc(namespaceEntity.get(NamespaceEntity_.code))); // Execute the query and return the result list. return entityManager.createQuery(criteria).getResultList(); }
Example 4
Source File: ActionListLikePinyin.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
private List<Wo> list(Business business, Wi wi) throws Exception { List<Wo> wos = new ArrayList<>(); if (StringUtils.isEmpty(wi.getKey())) { return wos; } List<String> roleIds = business.expendGroupRoleToRole(wi.getGroupList(), wi.getRoleList()); String str = StringUtils.lowerCase(StringTools.escapeSqlLikeKey(wi.getKey())); EntityManager em = business.entityManagerContainer().get(Role.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Role> cq = cb.createQuery(Role.class); Root<Role> root = cq.from(Role.class); Predicate p = cb.like(root.get(Role_.pinyin), str + "%"); p = cb.or(p, cb.like(root.get(Role_.pinyinInitial), str + "%")); if (ListTools.isNotEmpty(roleIds)) { p = cb.and(p, root.get(Role_.id).in(roleIds)); } List<Role> os = em.createQuery(cq.select(root).where(p)).getResultList(); wos = Wo.copier.copy(os); wos = business.role().sort(wos); return wos; }
Example 5
Source File: ActionListPinyinInitial.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
private List<Wo> list(Business business, Wi wi) throws Exception { List<Wo> wos = new ArrayList<>(); if (StringUtils.isEmpty(wi.getKey())) { return wos; } List<String> roleIds = business.expendGroupRoleToRole(wi.getGroupList(), wi.getRoleList()); String str = StringUtils.lowerCase(StringTools.escapeSqlLikeKey(wi.getKey())); EntityManager em = business.entityManagerContainer().get(Role.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Role> cq = cb.createQuery(Role.class); Root<Role> root = cq.from(Role.class); Predicate p = cb.like(root.get(Role_.pinyinInitial), str + "%", StringTools.SQL_ESCAPE_CHAR); if (ListTools.isNotEmpty(roleIds)) { p = cb.and(p, root.get(Role_.id).in(roleIds)); } List<Role> os = em.createQuery(cq.select(root).where(p)).getResultList(); wos = Wo.copier.copy(os); wos = business.role().sort(wos); return wos; }
Example 6
Source File: MoviesBean.java From tomee with Apache License 2.0 | 6 votes |
public List<Movie> getMovies(Integer firstResult, Integer maxResults, String field, String searchTerm) { CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Movie> cq = qb.createQuery(Movie.class); Root<Movie> root = cq.from(Movie.class); EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class); if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) { Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class)); Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%"); cq.where(condition); } TypedQuery<Movie> q = entityManager.createQuery(cq); if (maxResults != null) { q.setMaxResults(maxResults); } if (firstResult != null) { q.setFirstResult(firstResult); } return q.getResultList(); }
Example 7
Source File: StorageFileDaoImpl.java From herd with Apache License 2.0 | 6 votes |
@Override public Long getStorageFileCount(String storageName, String filePathPrefix) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); // The criteria root is the storage files. Root<StorageFileEntity> storageFileEntity = criteria.from(StorageFileEntity.class); // Join to the other tables we can filter on. Join<StorageFileEntity, StorageUnitEntity> storageUnitEntity = storageFileEntity.join(StorageFileEntity_.storageUnit); Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage); // Create path. Expression<Long> storageFileCount = builder.count(storageFileEntity.get(StorageFileEntity_.id)); // Create the standard restrictions (i.e. the standard where clauses). Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase()); Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path), String.format("%s%%", filePathPrefix)); // Add the clauses for the query. criteria.select(storageFileCount).where(builder.and(storageNameRestriction, filePathRestriction)); return entityManager.createQuery(criteria).getSingleResult(); }
Example 8
Source File: ScreenshotService.java From mojito with Apache License 2.0 | 6 votes |
private Predicate getPredicateForSearchType( SearchType searchType, CriteriaBuilder builder, Path<String> searchPath, String searchValue) { Predicate predicate = null; searchValue = NormalizationUtils.normalize(searchValue); if (searchType == null || SearchType.CONTAINS.equals(searchType)) { predicate = builder.like(searchPath, escapeAndWrapValueForContains(searchValue)); } else if (SearchType.ILIKE.equals(searchType)) { predicate = builder.like(builder.lower(searchPath), searchValue.toLowerCase()); } else { predicate = builder.equal(searchPath, searchValue); } return predicate; }
Example 9
Source File: MoviesBean.java From tomee with Apache License 2.0 | 5 votes |
public int count(String field, String searchTerm) { CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); Root<Movie> root = cq.from(Movie.class); EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class); Path<String> path = root.get(type.getDeclaredSingularAttribute(field, String.class)); Predicate condition = qb.like(path, "%" + searchTerm + "%"); cq.select(qb.count(root)); cq.where(condition); return entityManager.createQuery(cq).getSingleResult().intValue(); }
Example 10
Source File: StringFieldCriteria.java From onedev with MIT License | 5 votes |
@Override protected Predicate getValuePredicate(Join<?, ?> field, CriteriaBuilder builder) { Path<String> attribute = field.get(IssueField.PROP_VALUE); if (operator == IssueQueryLexer.Is) return builder.equal(builder.lower(attribute), value.toLowerCase()); else return builder.like(builder.lower(attribute), "%" + value.toLowerCase() + "%"); }
Example 11
Source File: ActionListPinyinInitial.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private List<Wo> list(EffectivePerson effectivePerson, Business business, Wi wi) throws Exception { List<Wo> wos = new ArrayList<>(); if (StringUtils.isEmpty(wi.getKey())) { return wos; } List<String> unitIds = business.expendUnitToUnit(wi.getUnitList()); /** 去掉指定范围本身,仅包含下级 */ unitIds.removeAll(ListTools.extractProperty(business.unit().pick(wi.getUnitList()), JpaObject.id_FIELDNAME, String.class, true, true)); String str = StringUtils.lowerCase(StringTools.escapeSqlLikeKey(wi.getKey())); EntityManager em = business.entityManagerContainer().get(Unit.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Unit> cq = cb.createQuery(Unit.class); Root<Unit> root = cq.from(Unit.class); Predicate p = cb.like(root.get(Unit_.pinyinInitial), str + "%", StringTools.SQL_ESCAPE_CHAR); if (ListTools.isNotEmpty(unitIds)) { p = cb.and(p, root.get(Unit_.id).in(unitIds)); } if (StringUtils.isNotEmpty(wi.getType())) { p = cb.and(p, cb.isMember(wi.getType(), root.get(Unit_.typeList))); } List<Unit> os = em.createQuery(cq.select(root).where(p)).getResultList(); wos = Wo.copier.copy(os); for (Wo wo : wos) { wo.setWoSupNestedUnitList(Wo.copier.copy(business.unit().listSupNestedObject(wo))); wo.setSubDirectUnitCount( business.entityManagerContainer().countEqual(Unit.class, Unit.superior_FIELDNAME, wo.getId())); wo.setSubDirectIdentityCount( business.entityManagerContainer().countEqual(Identity.class, Identity.unit_FIELDNAME, wo.getId())); } wos = business.unit().sort(wos); return wos; }
Example 12
Source File: JpaQueryUtils.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Generate a String comparison Predicate base on input parameters. * * Example: JpaUtils.stringPredicate( builder, root.get( "name" ), "%" + key + "%", JpaUtils.StringSearchMode.LIKE, false ) ) * * @param builder CriteriaBuilder * @param expressionPath Property Path for query * @param objectValue Value to check * @param searchMode JpaQueryUtils.StringSearchMode * @param caseSesnitive is case sensitive * @return a {@link Predicate}. */ private static Predicate stringPredicate( CriteriaBuilder builder, Expression<String> expressionPath, Object objectValue, StringSearchMode searchMode, boolean caseSesnitive ) { Expression<String> path = expressionPath; Object attrValue = objectValue; if ( !caseSesnitive ) { path = builder.lower( path ); attrValue = ((String) attrValue).toLowerCase( LocaleContextHolder.getLocale() ); } switch ( searchMode ) { 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!" ); } }
Example 13
Source File: MeetingFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public List<String> listPinyinInitial(String key) throws Exception { String str = key.replaceAll("_", "\\\\_"); str = str.replaceAll("%", "\\\\%"); str = str.toLowerCase(); EntityManager em = this.entityManagerContainer().get(Meeting.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> cq = cb.createQuery(String.class); Root<Meeting> root = cq.from(Meeting.class); Predicate p = cb.like(root.get(Meeting_.pinyinInitial), str + "%", '\\'); cq.select(root.get(Meeting_.id)).where(p); return em.createQuery(cq).getResultList(); }
Example 14
Source File: PredicateUtils.java From genie with Apache License 2.0 | 5 votes |
/** * Create either an equals or like predicate based on the presence of the '%' character in the search value. * * @param cb The criteria builder to use for predicate creation * @param expression The expression of the field the predicate is acting on * @param value The value to compare the field to * @return A LIKE predicate if the value contains a '%' otherwise an EQUAL predicate */ static Predicate getStringLikeOrEqualPredicate( @NotNull final CriteriaBuilder cb, @NotNull final Expression<String> expression, @NotNull final String value ) { if (StringUtils.contains(value, PERCENT)) { return cb.like(expression, value); } else { return cb.equal(expression, value); } }
Example 15
Source File: BuildingFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public List<String> listPinyinInitial(String key) throws Exception { String str = key.replaceAll("_", "\\\\_"); str = str.replaceAll("%", "\\\\%"); str = str.toLowerCase(); EntityManager em = this.entityManagerContainer().get(Building.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> cq = cb.createQuery(String.class); Root<Building> root = cq.from(Building.class); Predicate p = cb.like(root.get(Building_.pinyinInitial), str + "%", '\\'); cq.select(root.get(Building_.id)).where(p); return em.createQuery(cq).getResultList(); }
Example 16
Source File: MeetingFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public List<String> listLike(String key) throws Exception { String str = key.replaceAll("_", "\\\\_"); str = str.replaceAll("%", "\\\\%"); str = str.toLowerCase(); EntityManager em = this.entityManagerContainer().get(Meeting.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> cq = cb.createQuery(String.class); Root<Meeting> root = cq.from(Meeting.class); Predicate p = cb.like(root.get(Meeting_.subject), "%" + str + "%", '\\'); p = cb.or(p, cb.like(root.get(Meeting_.pinyin), str + "%", '\\')); p = cb.or(p, cb.like(root.get(Meeting_.pinyinInitial), str + "%", '\\')); cq.select(root.get(Meeting_.id)).where(p); return em.createQuery(cq).setMaxResults(200).getResultList(); }
Example 17
Source File: SimpleExpression.java From sctalk with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) { Path expression = null; if (fieldName.contains(".")) { System.out.println(root); String[] names = StringUtils.split(fieldName, "."); expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } } else { expression = root.get(fieldName); } switch (operator) { case EQ: return builder.equal(expression, value); case NE: return builder.notEqual(expression, value); case LIKE: return builder.like((Expression<String>) expression, "%" + value + "%"); case RLIKE: return builder.like((Expression<String>) expression, value + "%"); case LLIKE: return builder.like((Expression<String>) expression, value + "%"); case LT: return builder.lessThan(expression, (Comparable) value); case GT: return builder.greaterThan(expression, (Comparable) value); case LTE: return builder.lessThanOrEqualTo(expression, (Comparable) value); case GTE: return builder.greaterThanOrEqualTo(expression, (Comparable) value); case ISNULL: return builder.isNull(expression); case NOTNULL: return builder.isNotNull(expression); default: return null; } }
Example 18
Source File: SourceBranchCriteria.java From onedev with MIT License | 4 votes |
@Override public Predicate getPredicate(Root<PullRequest> root, CriteriaBuilder builder) { Path<String> attribute = root.get(PullRequest.PROP_SOURCE_BRANCH); String normalized = branch.toLowerCase().replace("*", "%"); return builder.like(builder.lower(attribute), normalized); }
Example 19
Source File: LikeRestriction.java From base-framework with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public Predicate build(Path expression, Object value,CriteriaBuilder builder) { return builder.like(expression, "%" + value + "%"); }
Example 20
Source File: GenericRsqlSpecification.java From tutorials with MIT License | 4 votes |
@Override public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) { final List<Object> args = castArguments(root); final Object argument = args.get(0); switch (RsqlSearchOperation.getSimpleOperator(operator)) { case EQUAL: { if (argument instanceof String) { return builder.like(root.get(property), argument.toString().replace('*', '%')); } else if (argument == null) { return builder.isNull(root.get(property)); } else { return builder.equal(root.get(property), argument); } } case NOT_EQUAL: { if (argument instanceof String) { return builder.notLike(root.<String> get(property), argument.toString().replace('*', '%')); } else if (argument == null) { return builder.isNotNull(root.get(property)); } else { return builder.notEqual(root.get(property), argument); } } case GREATER_THAN: { return builder.greaterThan(root.<String> get(property), argument.toString()); } case GREATER_THAN_OR_EQUAL: { return builder.greaterThanOrEqualTo(root.<String> get(property), argument.toString()); } case LESS_THAN: { return builder.lessThan(root.<String> get(property), argument.toString()); } case LESS_THAN_OR_EQUAL: { return builder.lessThanOrEqualTo(root.<String> get(property), argument.toString()); } case IN: return root.get(property).in(args); case NOT_IN: return builder.not(root.get(property).in(args)); } return null; }