JPA CriteriaBuilder case query

浪子不回头ぞ 提交于 2019-12-03 20:13:07

What follows is a sample case expression using CriteriaBuilder (this works in JPA 2):

Hashtable caseTable = new Hashtable(3);
caseTable.put("Bob", "Bobby");
caseTable.put("Susan", "Susie");
caseTable.put("Eldrick", "Tiger");
Expression expression = builder.get("firstName").caseStatement(caseTable, "NoNickname").equal("Bobby");

It generates the following SQL query:

"CASE t1.firstName WHEN 'Bob' THEN 'Bobby' WHEN 'Susan' THEN 'Susie' WHEN 'Eldrick' THEN 'Tiger' ELSE 'NoNickname' END = 'Bobby'"

For more info please see JPA 2.0 Case Expressions.

I could not find "caseStatement" in JPA 2.0 Criteria API. Seems its EclipseLink specific. The correct way is to use "builder.selectCase()"

See the section "Case Expressions" in Pro JPA 2: Mastering the Java Persistence API

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!