Nrusingha
Nrusingha

Reputation: 853

Excluding null properties in Criteria Hibernate

How to remove null properties from Criteria Hibernate. For example

select ename, empno, empDate from employee where ename = 'check' and empno= '1234' and empdate = to_date("22-jul-2000", "DD-MON-YYYY")

In the above example, ename or empno or empdate any property might be null.

How to implement in Criteria

I am not worried about the result set, I am worried about how the restrictions are added. I wan to avoid the following in the code:

if (((empno!= null) && (empno.trim().length()<=0)))
    {
        criteria.add(Restriction.eq("empno", empno))
    }

Upvotes: 1

Views: 2306

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42094

Generally you cannot remove single property from the result set, but you can set criteria that filters result out if certain attribute have null value:

criteria.add(Expression.isNotNull("ename"));

More details: Hibernate Chapter 16. Criteria Queries and here

Upvotes: 3

Related Questions