Case-insensitive equals using Hibernate Criteria

后端 未结 7 1473
旧时难觅i
旧时难觅i 2021-01-31 13:32

I\'ve seen Restrictions.ilike(\'property\', \'%value%\'), but would like to generate SQL like: lower(property) = \'value\'. Any ideas?

I used:

Restri         


        
相关标签:
7条回答
  • 2021-01-31 14:31

    If requirement is lower(property) = 'value' than best way is to use Restrictions.eq with ignoreCase() instead of using ilike cuz here the exact value is know. ilike is beneficial to search a pattern or when we are not aware of the exact value. Only problem with ignoreCase() is, it will not restrict your results to lower case and also include results with other cases , but i think is is the only available option hibernate offers.

     Criteria criteria = session.createCriteria(ClassName.class);
     criteria.add(Restrictions.eq("PROPERTY", propertyName).ignoreCase()).uniqueResult();
    

    Now if I understand your question correctly, your db has these values: ABC, abc, aBC, aBc...and so on, you want to convert them to lower case and then compare with some value say "ABC" or "abc". YOu can do this by further processing the list you got.

    You can save this result in a list.

    List<ClassName> listOfAllMatches =  criteria.list(); // gives all matched results (ABC, abc, aBC, aBc)
    
    
    
    List<ClassName> finalResult = listOfAllMatches.stream().map(x -> x.getPropertyName().toLowerCase()).filter(y ->y.getPropertyName()== value).collect(Collectors.toList());
    
    //convert this field to lower case using map() and filter your results with filter() function.
    

    YOU can further add this list to Criteria and process it.

    0 讨论(0)
提交回复
热议问题