我有代码:
public class CustomFilter<T> implements Specification<EntityHE> {
@Override
public Predicate toPredicate(Root<EntityHE> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
Predicate hasTenant = cb.equal(root.get("custom_field"), "value");
predicates.add(hasTenant);
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
}
@Entity(name = "entity")
@Table(name = "table")
public class EntityHE {
@Getter @Setter
@Column(name = "kind")
private String kind;
@Getter @Setter
@Column(name = "modified")
private Instant modified;
public EntityHE(){};
}
我可能会出现调用错误,因为我的实体中没有字段“custom_field”。 是否可以按此规范进行过滤,而无需在我的实体中添加“custom_field”作为字段?
最佳答案
不,这是不可能的。因为该规范将在 JPA Criteria API 查询中使用,并且您只能查询映射的属性。
关于java - 规范中是否可以使用非实体字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54195505/