简体   繁体   English

使用JPA 2.0 Criteria API执行类似Long值的操作

[英]Using JPA 2.0 Criteria API to perform like on Long value

I have following data structure stored in my database: 我有以下数据结构存储在我的数据库中:

public class Company{
    ...
    Long companyNumber;
    ...
}

Now I want to perform Like search on this particular column. 现在我想对此特定列执行Like搜索。 As stated in some similar questions , I'm trying to do a .as(String.class) on criteria builder inside my specification class: 正如一些类似的问题所述 ,我正在尝试在规范类中的条件构建器上执行.as(String.class)

public class CompanySpecification implements Specification<Company> { 

private String searchFilter;

@Override
public Predicate toPredicate(Root<Company> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    ...
    orPredicates.add(criteriaBuilder.like(root.get(Company_.companyNumber).as(String.class), "%" + searchFilter + "%"));
    ...
}

However the following implementation works only on the first character. 但是,以下实现仅适用于第一个字符。 For example: 例如:

If is search for string value 9 , this predicate returns all Companies where 9 is part of companyNumber ( 1119 , 1191 , 1911 , 9111 ) but when I search for values with more then one character, no results are found. 如果是搜索字符串值9 ,这个谓词返回所有公司,其中9是companyNumber的一部分( 1119119119119111 ),但是当我搜索值与一个以上的字符,没有结果发现。 For example: if i search for 91 , no results are found even though there are companies in db with company numbers containing 91 : 1191 , 1911 , 9111 . 例如:如果我搜索91 ,没有结果发现即使有公司分贝含公司编号91119119119111 I'm stuck on this, could you please point where have i made a mistake? 我坚持这个,你能指出我犯了什么错吗?

EDIT 编辑

added definition of searchFilter , search filter is set via constructor: 添加了searchFilter定义,搜索过滤器通过构造函数设置:

CompanySpecification companySpecification = new CompanySpecification(searchParameter);

where search parameter is passed as request parameter 其中search参数作为请求参数传递

原来上面的代码工作正常,问题是搜索是用分页执行的,请求页面的数量高于最大结果,因此返回的结果列表为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM