如何为 Hibernate 中的命名参数将 Null 值设置为 query.setString?
谢谢, 凯瑟尔
我认为你不能这样做,因为在 SQL 中你需要不同的空值语法。SQL 中的示例:
select x.col from table x where x.string = 'text';
但是你有一个空值
select x.col from table x where x.string is null;
因此,您不能在 SQL 的命名参数中设置 null。同样在 Hibernate 中,当您的字符串为空时,您需要一个不同的查询。例如:
String select;
if (s == null) {
select = "from Table where column is null";
} else {
select = "from Table where column = :string";
}
Query q = session.createQuery(s);
if (s != null) {
q.setString("string", s);
}