1

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

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());
4

1 回答 1

3

使用CriteriaBuilder#lower()

Predicate predicate = cb.like(cb.lower(emp.get(EmployeeDetail_.empName)),
            empName.toLowerCase() + "%");
于 2013-01-15T08:53:46.993 回答