How does hibernate use an empty string for an equality restriction?

最后都变了- 提交于 2019-12-05 06:34:44

With HSQL (and Derby) and the following values:

insert into FOO values (1, 'foo');
insert into FOO values (2, 'bar');
insert into FOO values (3,  NULL);
insert into FOO values (4, '');

You criteria query

Criteria crit = session.createCriteria(Foo.class);
crit.add(Restrictions.or(Restrictions.isNull("name"), 
                         Restrictions.eq("name", "")));
crit.list();

returns:

Foo [id=3, name=null]
Foo [id=4, name=]

As expected.

What database are you using? Could it be Oracle?

It seems like you're doing it wrong. null in Java maps to NULL in SQL, and empty String ("") in Java maps to empty string in SQL ('')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!