I use List
objects in Restrictions.in
methods, now I must use this case at Restrictions.like
But Restrictions.like
doesn't receive List
param. How Can I solve this problem?
My code is bottom:
public void setPhones(Set<String> phones) {
this.phones.addAll(phones);
if (!this.phones.isEmpty()) {
aliases.add(new QueryAlias("profile", "profile"));
criterions.add(Restrictions.like("profile.phone", this.phones));
}
}
Correcting my prior (and now edited) answer
According to documentation (https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/criterion/Restrictions.html) it seems you don't have a direct way to do this.
You can try iterating your List, then create a List of Restriction.like for each of your phones then converting this list into an array to use into a Restrictions.or:
public void setPhones(Set<String> phones) {
this.phones.addAll(phones);
if (!this.phones.isEmpty()) {
// Creates a list to store criterions
List<Criterion> criterionsPhoneNumbers = new ArrayList<>();
// For each number searched, it creates a criterion with a "Like Restriction" adding to criterionsPhoneNumbers List.
// Pay attention to match mode (in raw sql it'll be a "like" using %phoneNumber% - check the generated SQL by hibernate).
// You can change this to suit your needs.
for (String number : numbers) {
aliases.add(new QueryAlias("profile", "profile"));
criterionsPhoneNumbers.add( Restrictions.like("number", number, MatchMode.ANYWHERE) ) ;
}
// Here criterionsPhoneNumbers is being converted to array then added to the criteria with "Or Restriction"
criteria.add(Restrictions.or( criterionsPhoneNumbers.toArray(new Criterion[restrictionsPhoneNumbers.size()]) ));
}
}
My prior answer was wrong because adding each phone number as a Restriction.like (only) wasn't enough and was converted to a sql with a logical 'and' in 'where' clause. As I hadn't tested then I couldn't see the error.
I had implemented then I saw the error.
My apologies.