Search code examples
javahibernateormhibernate-criteria

Hibernate Criteria OR Restriction - fetch list if either one or both columns value is 1


My Table structure is as follows:

enter image description here

Can someone help with the following criteria query:

Criteria c = getSession().createCriteria(getEntity(), "b");
c.add(Restrictions.in("b.isTwitterVerified", 1));
or // b.isFbVerified =1// something like this
return c.list();

Solution

  • This should return what your after:

    Criteria criteria = getSession().createCriteria(getEntity(), "b"); 
    Criterion isTwitterVerified = Restrictions.eq("b.isTwitterVerified", 1);
    Criterion isFbVerified = Restrictions.eq("b.isFbVerified ", 1);
    criteria.add(Restrictions.or(isTwitterVerified , isFbVerified ));
    
    return criteria.list();
    

    On the side.. the in restriction that you tried to use: Restrictions.in.. this would have its place when you would like to verify a certain field against many values (1,3,5 for example).