简体   繁体   中英

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.

I am using EclipseLink v2.1 as JPA

Thanks in advnace.

EclipseLink Tutorial

You are using o.assigneeEmployee IS NOT NULL, and I assume o.assigneeEmployee is a relationship. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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