Java Spring Data JPA - 规范加入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39326893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:03:07 来源:igfitidea点击:
Spring Data JPA - Specifications join
提问by ByeBye
I have specification:
我有规格:
final String text = "%text%";
final Specifications<PersonEntity> spec = Specifications.where(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.addressLine1)), text)
).or(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.addressLine2)), text)
).or(
(root, query, builder) -> builder.like(builder.lower(root.join(PersonEntity_.addresses, JoinType.LEFT).get(AddressEntity_.city)), text)
)
After using:
使用后:
personRepository.findAll(spec);
In logs, I see, that JPA create a query where it joins addresses three times instead of once.
在日志中,我看到 JPA 创建了一个查询,它在其中连接地址三次而不是一次。
How can I write a Specification where addresses will be joined only once?
如何编写地址仅加入一次的规范?
采纳答案by ByeBye
I changed it to:
我把它改成:
Specifications.where(
(root, query, builder) -> {
final Join<PersonEntity, AddressEntity> addresses = root.join(PersonEntity_.address, JoinType.LEFT);
return builder.or(
builder.like(builder.lower(addresses.get(AddressEntity_.addressLine1)), text),
builder.like(builder.lower(addresses.get(AddressEntity_.addressLine2)), text),
builder.like(builder.lower(addresses.get(AddressEntity_.code)), text),
);
}
);
Now, it is joining only once.
现在,它只加入一次。