Hi,
I wonder which way to go with the [B/b]oolean choice in Hibernate.
My Dao layer is built using the Criteria API.
At first, I took the boolean way, the primitive type.
It worked fine.
I could have a Criteria that looked like:
Code:
public List<Webpage> findAllNotInGarbage() {
Criteria criteria = getSession().createCriteria(Webpage.class);
criteria.add(Restrictions.eq("garbage", false));
criteria.addOrder(Order.asc("name"));
return criteria.list();
}
But then, I read that it's better to take the Boolean way, the complex type.
And so, the property is then nullable.
A "false" might contain the false value or the null value.
A "true" does contain the true value.
Therefore, when checking against "false" values, the Criteria needs to check for both false and null values.
Code:
public List<Webpage> findAllNotInGarbage() {
Criteria criteria = getSession().createCriteria(Webpage.class);
criteria.add(Restrictions.or(Restrictions.eq("garbage", false), Restrictions.isNull("garbage")));
criteria.addOrder(Order.asc("name"));
return criteria.list();
}
And when retrieving a value as in:
Code:
webpage.getGarbage();
the following assertion might be failing if the value is null instead of false:
Code:
assertEquals(false == webpage.getGarbage());
and this one might succeed:
Code:
assertNull(webpage.getGarbage());
In the second way, I therefore end up with a more complex Criteria, and an uncertainty, that is, two possible values for a "false", either false or null.
What do you think ?
Which [B/b]oolean way would you go ?
Cheers,