【发布时间】:2021-09-10 17:57:57
【问题描述】:
我正在尝试使用 spring 规范搜索视图,但无法使 or 运算符工作。
这是我的实体
@Entity
@Getter
@Immutable
public class EntityA {
@Id
@GeneratorValue(strategy = GenerationType.IDENTITY)
private Integerid;
private String content;
@ManyToOne
private EntityB entityB;
@ManyToOne
private EntityC entityC;
}
我的方法
Page<EntityA> page = repository.findAll(EntityARepository.specification(id), pageable);
我在 EntityARepository 中的规范方法
static Specification<EntityA> specification(int id) {
return (tx, cq, cb} -> {
Predicate predicateB = cb.equals(tx.get("entityB").get("id"), id);
Predicate predicateC = cb.equals(tx.get("entityC").get("id"), id);
List<Predicate> predicates = Lists.of(predicateB, predicateC);
return cb.or(predicates.toArray(new Predicate[0]));
}
我也尝试编写 2 个单独的规范方法并将它们与 Specification.where(specB).or(specC); 结合,但它也不起作用。
但是,当我使用基本的 Spring Data Jpa 方法 FindAllByEntityBIdOrEntityCId(int idB, int idC) 时,它运行良好
我无法弄清楚我做错了什么。我得到一个空的结果。
【问题讨论】:
-
当你使用 spring.jpa.show-sql=true (application.properties) 时会发生什么?
标签: java spring hibernate spring-mvc spring-data-jpa