如何在HQL中模拟NVL

30 hibernate hql

我试过这个:

from Table where (:par1 is null or col1 = :par1)
Run Code Online (Sandbox Code Playgroud)

但它发生了

from Table where :par1 is null
Run Code Online (Sandbox Code Playgroud)

始终返回表的所有行,即使:par1不为null.

 select * from table where col1 = 'asdf'
Run Code Online (Sandbox Code Playgroud)

不会返回任何行.

我不能使用本机语法,因为我的应用程序应该在不同的数据库引擎上运行

Il-*_*ima 62

nvl与HQL中的coalesce命令等效的是命令. 如果不为null coalesce(a,b)则返回,否则返回.aab

所以你会想要的东西:

from Table where col1 = coalesce(:par1, 'asdf')
Run Code Online (Sandbox Code Playgroud)

  • 我想要的实际上是:从表中col1 = coalesce(:par1,col1)并且它有效!非常感谢! (2认同)
  • coalesce()也是一个ANSI SQL函数,应该适用于大多数数据库,例如Oracle,本机. (2认同)