Jack
Jack

Reputation: 6620

How to verify criteria.list is empty?

I've created a criteria to retrieve list of items. Although as expected, no result will be returned the if condition does not work.

Criteria cre = session.createCriteria(Name.class,"name);
cre.add(Restrictions.eq("name.fname","Alex");
List<Name> names = (List<Name>) cre.list();

I used both following 'if' conditions but neither works

1) if(names.isEmpty())
       System.err.println("cre is empty"); 

2) if(names != null)
       System.err.println("cre is empty"); 

Upvotes: 1

Views: 1422

Answers (2)

Martin Olivera
Martin Olivera

Reputation: 48

Simplest way

cre.list().isEmpty()

Upvotes: 0

Jordan.J.D
Jordan.J.D

Reputation: 8113

You can use the size() of the list. If the list's size is 0, it is empty.

if(names.size() == 0){
//it is empty
}

if(names.size() > 0){
//it is **not** empty
}

Upvotes: 1

Related Questions