简体   繁体   中英

Hibernate Restriction to search with like clause in list

I need help with hibernate criteria restriction.

As we know that we use IN restriction with list , ie

Criteria criteria = session.createCriteria(Student.class);
List<String> studentNames= new ArrayList();
criteria.add(Restrictions.in("name",studentNames);
List list = criteria.list();

and to use like we use restriction like this ie

String matchString = "Suraj Kumar";
criteria.add(Restrictions.like(
"studentName", matchString));

Problem: What i want is that every element in list should be searched with "LIKE" clause. If we use IN clause, then it compares the element with all the elements passed in the IN clause, and that comparison is based on equality.

But requirement is that , every element in the list should be checked with the LIKE clause , with the corresponding database column.

Please help me to create hibernate criteria restriction with the same.

Thanks

You can try to use Disjunction which can be used if you have multiple OR conditions.

    Criteria criteria = session.createCriteria(Student.class);
    List<String> studentNames= new ArrayList();
    Disjunction disj = Restrictions.disjunction();
    for (String value : studentNames) {
        disj.add(Restrictions.like("name", value));
    }
    criteria.add(disj);
    List list = criteria.list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM