【发布时间】:2019-05-29 20:24:34
【问题描述】:
我正在尝试编写一个具有 3 个条件 where 语句的 JPA 查询。但我似乎没有得到正确的语法。
try {
Query findCustomerQuery = entityManager.createQuery("SELECT c FROM Customer c WHERE " +
"CASE " +
"WHEN :customerNumber is not null THEN (c.customerNumber = :customerNumber)" +
"WHEN :customerName is not null THEN (c.name = :customerName) " +
"WHEN :customerFirstname is not NULL THEN (c.firstName = :customerFirstname) ELSE (c.customerNumber = 0) END");
findCustomerQuery.setParameter("customerNumber",customerNumber);
findCustomerQuery.setParameter("customerName", customerName);
findCustomerQuery.setParameter("customerFirstname", customerFirstname);
return (Customer) findCustomerQuery.getSingleResult();
}finally {
entityManager.close();
}
【问题讨论】:
-
在哪里? CASE 用于 SELECT 而不是 WHERE
标签: java jpa eclipselink