gpt4 book ai didi

java - 使用元模型区分大小写条件的 JPA 2 Criteria API

转载 作者:行者123 更新时间:2023-11-29 05:49:55 25 4
gpt4 key购买 nike

我有以下代码行来使用 Hibernate 4 API 基于 like 语句获取结果

Predicate predicate = cb.like(emp.get(EmployeeDetail_.empName),
empName+"%");

生成的sql语句为

 select employeede0_.EMPLOYEE_NAME as EMPLOYEE1_0_ from EMPLOYEES employeede0_ 
where employeede0_.EMPLOYEE_NAME like 'smith%'

如何修改我的 java 代码以使用小写的 EMPLOYEE_NAME?生成的sql输出应该是这样的

select employeede0_.EMPLOYEE_NAME as EMPLOYEE1_0_ from EMPLOYEES employeede0_ 
where lower(employeede0_.EMPLOYEE_NAME) like lower('smith%')

获取结果的完整代码

   CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
Root<Employee> emp = c.from(Employee.class);
c.select(emp);
List<Predicate> criteria = new ArrayList<Predicate>();
ParameterExpression<String> pexp = cb.parameter(String.class,
"empName");
Predicate predicate = cb.like(emp.get(Employee_.empName),
empName+"%");
criteria.add(predicate);

if (criteria.size() == 1) {
c.where(criteria.get(0));
} else if (criteria.size() > 1) {
c.where(cb.and(criteria.toArray(new Predicate[0])));
}
TypedQuery<EmployeeDetail> q = entityManager.createQuery(c);
data.setResult(q.getResultList());

最佳答案

使用CriteriaBuilder#lower() :

Predicate predicate = cb.like(cb.lower(emp.get(EmployeeDetail_.empName)),
empName.toLowerCase() + "%");

关于java - 使用元模型区分大小写条件的 JPA 2 Criteria API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com