Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java hibernate Restriction.ne not include entities with null

Tags:

java

hibernate

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();
}
like image 516
Edgaras Karka Avatar asked Feb 23 '26 03:02

Edgaras Karka


2 Answers

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.

like image 193
Dragan Bozanovic Avatar answered Feb 25 '26 16:02

Dragan Bozanovic


The criteria is well written, it should work .

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

like image 32
Jorge Jiménez Barra Avatar answered Feb 25 '26 16:02

Jorge Jiménez Barra