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

Posted by Stephen on Stack Overflow See other posts from Stack Overflow or by Stephen
Published on 2010-05-11T17:53:52Z Indexed on 2010/05/12 9:24 UTC
Read the original article Hit count: 202

Filed under:
|
|

I have a column that potentially has some bad data and I can't clean it up, so I need to check for either null or empty string. I'm doing a Hibernate Criteria query so I've got the following that returns incorrectly right now:

Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Criteria myCriteria = session.createCriteria(Object);
...
myCriteria.add(Restrictions.or(Restrictions.isNull("stringColumn"),
                               Restrictions.eq("stringColumn", "")));
List<Objects> list = myCriteria.list();

I can't get it to properly return the results I'd expect. So as an experiment I changed the second restriction to read:

 Restrictions.eq("stringColumn", "''") 

And it started returning the expected results, so is hibernate incorrectly translating my empty string (e.g. "") into a SQL empty string (e.g. ''), or am I just doing this wrong?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about criteria