Home > front end >  QueryDSL implementation of SQL's ISNULL function
QueryDSL implementation of SQL's ISNULL function

Time:01-07

Is there a way to write the SQL function ISNULL in a JPAQuery using QueryDSL when it's part of the selected data? For example, I have the native SQL query:

SELECT ISNULL(date1, date2) AS date_field FROM TABLE..

Currently, the only option I see is to return both date1 and date2 in my JPAQuery, and then do my own check in Java code to know which one to access. But it would be nice to have to return multiple fields when I don't need to.

I think it would look like this:

return new JPAQuery<ResponseObject>(entityManager)
     .select(Projections.bean(ResponseObject.class, 
             table.date1,
             table.date2))
     .from(table)
     .fetch();

Sorry if I've confused some areas of JPA with QueryDSL or vice versa.

CodePudding user response:

You can simply use coalesce that is supported by QueryDSL and will work same as ISNULL

For example:

new Coalesce<>(Date.class)
   .add(qEntity.firstOptional)
   .add(qEntity.secondOptional)
  • Related