参数化 HQL 'is null' 查询变为 '= null'

Parameterised HQL 'is null' query becomes '= null'

我有一个查询构建器,它构建一个参数化的 HQL 查询来进行 OData 过滤。当针对 属性 是 NULL 构建查询时,HQL 的 where 子句看起来像这样...

... where $entity.Property is ?

...其中 ? 被值为 NULL 的参数替换。但是,当我使用上述查询调用 session.CreateQuery(hql) 时,执行的 SQL 不是 is null 查询,而是变成 =@p1 查询 @p1 填充到为空。这显然不符合我的要求。

如果我明确地制作 HQL $entity.Propery is null 那么一切正常,但我觉得我遗漏了什么或做了一些愚蠢的事情。

这是怎么回事?

根据这个 answer IS 不是运算符。

Notice something important, there. There is no such thing as the "IS" operator in T-SQL. There is specifically the IS [NOT] NULL operator, which compares a single expression to NULL.

因此,如果您想使用 IS NULL,您可能需要创建一个动态查询,或者使用您的参数,例如 ? IS NULL