67

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

4 Answers 4

107

That is a binary operator in hibernate you should use

is not null

Have a look at 14.10. Expressions

Sign up to request clarification or add additional context in comments.

Comments

44

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

Comments

18

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

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.