Edgaras Karka
Edgaras Karka

Reputation: 7852

Java hibernate Restriction.ne not include entities with null

I try get a list by criteria using Restrictions.ne("status","ERROR") but method returns the list without entities where status is null.

public List<Match_soccer> getDayMatches(Date day){
    //Match_soccer where date between start and end dates and status != null
    Criteria criteria = session.createCriteria(Match_soccer.class);
    criteria.add(Restrictions.between("start", day, DateJobs.addnHours(DateJobs.nextDay(day), 3)));
    criteria.add(Restrictions.ne("status","ERROR"));
    return criteria.list();
}

Upvotes: 3

Views: 2529

Answers (2)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

Include an explicit null check:

criteria.add(Restrictions.or(
   Restrictions.ne("status","ERROR"),
   Restrictions.isNull("status"))
);

The point is that most databases evaluate comparison operators with null arguments to false, meaning that both status = null and status <> null are considered false.

Upvotes: 8

The criteria is well written, it should work .

Have you tried to remove the date filter to see if that is filtering "status=null" ?

Upvotes: 1

Related Questions