简体   繁体   English

jpql中select的case语句

[英]case statement in select in jpql

I have tried the following but not working. 我已经尝试了以下方法,但无法正常工作。

String mainQuerySt = "select o.progressStatus, "
+ " CASE WHEN o.assigneeEmployee IS NOT NULL THEN o.assigneeEmployee.fullNameSt ELSE '' END as assignee "
+ " from Tt o"

em.createQuery(mainQuerySt).getResultList();

What is wrong with this? 这有什么问题? Actually, I want to show assigneeEmployee full name if it is not null and otherwise an empty string. 实际上,如果不为null,则要显示AssigneeEmployee的全名,否则为空字符串。

I am using EclipseLink v2.1 as JPA 我正在将EclipseLink v2.1用作JPA

Thanks in advnace. 提前感谢。

EclipseLink Tutorial EclipseLink教程

You are using o.assigneeEmployee IS NOT NULL, and I assume o.assigneeEmployee is a relationship. 您正在使用o.assigneeEmployee IS NOT NULL,并且我假设o.assigneeEmployee是一种关系。 Using dot notation forces an inner join, which then will filter out nulls. 使用点表示法会强制进行内部联接,然后将其过滤出空值。 Try 尝试

String mainQuerySt = "select o.progressStatus, "
+ " CASE WHEN assigneeEmployee IS NOT NULL THEN assigneeEmployee.fullNameSt ELSE '' END as assignee "
+ " from Tt o left outer join o.assigneeEmployee assigneeEmployee"

If it does not return the results expected, then you will need to turn on SQL logging to see the SQL produced, and show the results you do expect. 如果未返回预期结果,则需要打开SQL日志记录以查看生成的SQL,并显示预期结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM