1

如何为 Hibernate 中的命名参数将 Null 值设置为 query.setString?

谢谢, 凯瑟尔

4

1 回答 1

0

我认为你不能这样做,因为在 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);
}
于 2012-09-06T08:22:36.383 回答