如何在 Restrictions.like 中使用 List<String> 参数
How to use List<String> params in Restrictions.like
我在 Restrictions.in
方法中使用了 List
个对象,现在我必须在 Restrictions.like
中使用这种情况
但是 Restrictions.like
没有收到 List
参数。我怎么解决这个问题?
我的代码在底部:
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));
}
}
更正我之前(现在已编辑)的回答
根据文档 (https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/criterion/Restrictions.html),您似乎没有直接的方法来执行此操作。
您可以尝试迭代您的列表,然后创建 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()]) ));
}
}
我之前的回答是错误的,因为将每个 phone 数字添加为 Restriction.like(仅)是不够的,并且被转换为具有逻辑 'and' 的 sql ] 在 'where' 子句中。因为我没有测试,所以我看不到错误。
我已经实施然后我看到了错误。
抱歉。
我在 Restrictions.in
方法中使用了 List
个对象,现在我必须在 Restrictions.like
中使用这种情况
但是 Restrictions.like
没有收到 List
参数。我怎么解决这个问题?
我的代码在底部:
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));
}
}
更正我之前(现在已编辑)的回答
根据文档 (https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/criterion/Restrictions.html),您似乎没有直接的方法来执行此操作。
您可以尝试迭代您的列表,然后创建 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()]) ));
}
}
我之前的回答是错误的,因为将每个 phone 数字添加为 Restriction.like(仅)是不够的,并且被转换为具有逻辑 'and' 的 sql ] 在 'where' 子句中。因为我没有测试,所以我看不到错误。
我已经实施然后我看到了错误。
抱歉。