英文:
Add specification from a loop
问题
以下是代码部分的中文翻译:
在一个Spring Boot 3应用程序中,我尝试使用规约(Specification)。
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
...
return pre;
};
return findAll(specification, page);
}
我需要为splittedValues中的每个值添加一个全局规约。
for (String splittedName : splittedValues) {
specification.or(splittedName);
}
并将其传递给findAll。
我不明白如何做到这一点。
编辑
你的解决方案似乎有效,但会生成以下查询:
where
1=1
or like e1_0.name "%bob%"
or like e1_0.name "%jame%"
有没有办法得到以下查询:
where
1=1
and (
like e1_0.name "%bob%"
or like e1_0.name "%jame%"
)
请注意,这个翻译只包括代码部分,不包括其他内容。
英文:
In a spring boot 3 application I try to use specification
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
...
return pre;
};
return findAll(specification, page);
}
I need for every value in splittedValues to add a global specification
for (String splittedName: splittedValues) {
specification.or(splittedName);
}
and pass it to findAll
I don't understand how to do it
Edit
you solution seem to work but
that generate
where
1=1
or like e1_0.name "%bob%"
or like e1_0.name "%jame%"
It's there a way to get
where
1=1
and (
like e1_0.name "%bob%"
or like e1_0.name "%jame%"
)
答案1
得分: 1
使用 or
运算符,您可以组合多个条件,检查 User
的 name
是否包含 splittedValues
中的一个。然后,从规约返回谓词。
希望这是您想要的,如果我理解错了,请纠正我。如果是这样,请参照以下示例进行调整以适应您自己的代码。
编辑:要获取 1=1 and (like e1_0.name "%bob%" or like e1_0.name "%jame%")
,您可以使用 cb.or
方法组合条件,然后使用 cb.and
将 nameConditions
与 pre
结合。
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
Predicate pre = cb.conjunction();
Predicate nameConditions = null;
for (String splittedName : splittedValues) {
if (nameConditions == null) {
nameConditions = cb.like(root.get("name"), "%" + splittedName + "%");
} else {
nameConditions = cb.or(nameConditions, cb.like(root.get("name"), "%" + splittedName + "%"));
}
}
if (nameConditions != null) {
pre = cb.and(pre, nameConditions);
}
return pre;
};
return findAll(hasPersonWithName, page);
}
请注意,这是您提供的代码的翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。
英文:
Using or
operator , you combine multiple conditions, checking whether the name
of User
contains one of the splittedValues
. Predicate is then returned from specification.
I hope that was what you wanted, in case I didn't understand, please correct me. Here is an example if so, adapt to your own code.
Edit: To get 1=1 and (like e1_0.name "%bob%" or like e1_0.name "%jame%")
, you can use cb.or
method to combine the conditions, resulting nameConditions
is then combined with pre
using cb.and
.
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
Predicate pre = cb.conjunction();
Predicate nameConditions = null;
for (String splittedName: splittedValues) {
if (nameConditions == null) {
nameConditions = cb.like(root.get("name"), "%" + splittedName + "%");
} else {
nameConditions = cb.or(nameConditions, cb.like(root.get("name"), "%" + splittedName + "%"));
}
}
if (nameConditions != null) {
pre = cb.and(pre, nameConditions);
}
return pre;
};
return findAll(hasPersonWithName, page);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论