JPA: How to check NULL query-parameter value with HQL?

See example in Hibernate documentation.

You should check is parameter is null:

Query query =  em.createQuery(
        "select count(u) from User u where (u.id=:userId) and (u.photo is null)")
        .setParameter("userId",userId);

If you're aiming at generalising "b", so that a method can receive either a given search value, or a null, this usually works:

Query q = em.createQuery (
  "SELECT ... WHERE ... ( :b IS NULL AND u.photo IS NULL OR u.photo = :b )" 
);
q.setParameter ( "b", searchValue ); // can be null
...

There is an helper to use this approach here (see parameterizedWithNullHql()).

Tags:

Hibernate

Jpa