SQL return 如果 Boolean 为 null,则为所有值

SQL return all the values if Boolean is null

这可能是一个愚蠢的问题,但我被困在这个问题上了。我在休眠中有一个方法如下

public List<User> searchUser(String name, Boolean booleanCheck) {
    Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from User user where user.booleanCheck = :booleanCheck");
    return hqlQuery.setParameter("booleanCheck", booleanCheck).list();
}

我需要的是,如果 booleanCheck 参数为 null,它应该 return 所有值。如果我有真值或假值,它应该 return 所选值。

目前它return如果为空则没有任何值。我可以做些什么来获得理想的输出吗?

提前致谢

尝试在 SQL 中使用常用的快捷方式来简化参数:

Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from User user where (:booleanCheck is NULL or user.booleanCheck = :booleanCheck)");

当值为空时,您必须区分您的查询。在您的实现中,生成的查询是

select * from User user where user.booleanCheck = null;

这确实什么也找不到。当 booleanCheck 为 null 时,您必须生成查询

select * from User user where user.booleanCheck is null;