gpt4 book ai didi

java - Spring Data JPA - 规范加入

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:42 24 4
gpt4 key购买 nike

我有规范:

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)
)

使用后:

personRepository.findAll(spec);

在日志中,我看到 JPA 创建了一个查询,它在其中将地址连接了三次而不是一次。

我怎样才能写一个地址只加入一次的规范?

最佳答案

我把它改成了:

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),
);
}
);

现在,它只加入一次。

关于java - Spring Data JPA - 规范加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39326893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com