为Hibernate中的命名参数设置query.setString的Null值



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

谢谢,Kathir

我认为你做不到,因为在SQL中,你需要一种不同的null值语法。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中,当字符串为null时,您需要一个不同的查询。例如:

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);
}

最新更新