是否可以在 HQL 查询中选择 null 作为列值?

2022-09-04 01:28:17

我需要一个包含三列的列表。列 1st 和 3rd 具有值,而 2nd 列为 null。我可以通过 HQL 查询来做吗?

我需要这样的东西:

select id, null, name from MyClass

其中 MyClass 和基础表只有两个属性/列,即“id”和“name”


答案 1

另一个似乎有效的选项(在DB2,MySQL,Oracle和SQL Server上进行了测试):

select id, cast(null as char), name from ...

您可以对 Hibernate 方言进行子类化,并使用自定义函数对其进行抽象:

registerFunction("always_null", 
  new SQLFunctionTemplate(Hibernate.STRING, "cast(null as char)"));

然后在您的 HQL 中使用它:

select id, always_null(), name from ...

答案 2

您可以使用函数生成空值。nullif()

它接受两个论点。如果参数相等,它将返回值。null


推荐