0

I have an use-case where passing parameters is optional. Can this be somehow achieved as if I try to set parameter (Using keyName or position) and if the key or position is absent in main query it returns IllegalParameterException.

This is useful if I have a dynamic query coming into method and want's to set some default values if possible.

Say:

Select ............ where date = :date

And if we have parameter value set for key "date" that will be used.

In other cases

Select .............. where date = NOW()

Here even though we have value set for key "date", we don't need to use

Any suggestion is welcome!

Jake
  • 391
  • 1
  • 4
  • 22
  • 1
    Try `(:date IS NULL && date=NOW()) || (date = :date)` – Grim Oct 01 '20 at 12:24
  • https://stackoverflow.com/questions/50569238/set-optional-parameters-in-jpql-query – yazabara Oct 01 '20 at 12:33
  • Thanks @Grim, this works, but still if I set parameter which is not on query it throws an error. Btw - how to deal with "between :abc AND :xyz" situations! – Jake Oct 01 '20 at 13:35
  • Jake, yes I missed that point. I guess this is unspecified and can cause exception on drivers on different sql-driver-implementations. – Grim Oct 01 '20 at 15:43

0 Answers0