JPA CriteriaBuilder case query

ぐ巨炮叔叔 提交于 2019-12-09 13:42:08

问题


Can anyone provide an example of how to write a case query using CriteriaBuilder?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5598656/jpa-criteriabuilder-case-query

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