65

Does hibernate convert column != null in HQL to a column is null in SQL?

xrcwrn
  • 5,339
  • 17
  • 68
  • 129
Eric V
  • 1,150
  • 1
  • 9
  • 24

4 Answers4

105

That is a binary operator in hibernate you should use

is not null

Have a look at 14.10. Expressions

Eduard
  • 3,176
  • 3
  • 21
  • 31
43

No. You have to use is null and is not null in HQL.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
15

If you do want to use null values with '=' or '<>' operators you may find the

answer from @egallardo hier

very useful.

Short example for '=': The expression

WHERE t.field = :param

you refactor like this

WHERE ((:param is null and t.field is null) or t.field = :param)

Now you can set the parameter param either to some non-null value or to null:

query.setParameter("param", "Hello World"); // Works
query.setParameter("param", null);          // Works also
Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
  • 1
    Right answer, but a bit overkill :) I do it if I have no other solutions: If there is only one or two nullable param, i find it better to make many HQL or JPQL queries. – pdem Jan 19 '17 at 11:22
1

No. See also this link Handle conditional null in HQL for tips and tricks on how to handle comparisons with both null and non-null values.

Community
  • 1
  • 1
egallardo
  • 1,234
  • 1
  • 15
  • 25