Fab*_* B. 4 hibernate jpa criteria-api jpa-2.0
我想使用"name + surname"字符串作为键来查询我的"customers"表.
姓名和姓氏存储在不同的字段中.
所以我的查询是:
SELECT
*
FROM
customers
WHERE
CONCAT(name,' ',surname) LIKE '%term%'
OR CONCAT(surname,' ',name) LIKE '%term%'
Run Code Online (Sandbox Code Playgroud)
但是,我不能这样做,我的查询是JPA2 条件查询.就像是:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Customer> from = cq.from(Customer.class);
cq.select(from);
Predicate whereClause = cb.or(
cb.like(from.<String>get("name"), "%"+ r +"%"),
cb.like(from.<String>get("surname"), "%"+ r +"%"),
);
cq.where(whereClause);
TypedQuery<Customer> query = getEntityManager().createQuery(cq);
list = query.getResultList();
Run Code Online (Sandbox Code Playgroud)
如何通过姓名和姓氏的组合过滤我的结果集,好吗?
per*_*ssf 17
使用CriteriaBuilder.concat(表达式,表达式):
Expression<String> exp1 = cb.concat(from.<String>get("name"), " ");
exp1 = cb.concat(exp1, from.<String>get("surname"));
Expression<String> exp2 = cb.concat(from.<String>get("surname"), " ");
exp2 = cb.concat(exp2, from.<String>get("name"));
Predicate whereClause = cb.or(cb.like(exp1, "%"+ r +"%"), cb.like(exp2, "%"+ r +"%"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10512 次 |
| 最近记录: |