Specification如何写一个多表查询的Predicate的方法呢?jpa的

发布于 2022-09-02 23:37:05 字数 716 浏览 14 评论 0

@Override
            public Predicate toPredicate(Root<One> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

       List<Predicate> list = new ArrayList<Predicate>();
       
       //如下方法是查询One里面的单一内容,这样构成了一个条件
       if (searchParams.get("bus_like") != null) {

        list.add(cb.like(root.get("busNum").as(String.class), "%" + searchParams.get("bus_like") + "%"));

                }

        //假设我需要在此查询Two和Three表的内容,如何弄呢?
        
        


        Predicate[] p = new Predicate[list.size()];

        return cb.and(list.toArray(p));
}

如图,在总的方法中,已经定义Root为One了。那么,如果再接下来在方法内写一个Two、Three多表查询的Predicate.......??比如,我需要知道表Two中的权限是否对应One中的数据,然后决定是否显示呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂ガ小子 2022-09-09 23:37:05

假设有 A,B,C三张表
class A{

private List<B> bs= new ArrayList<>();

}
class B{

private A a = new A();
private C c = new C();

}
class C{

private Long id;
private List<B> bs= new ArrayList<>();

}
目的是 通过C 查询到A的相关内容
当前root 是 A
Join<A, B> join = root.join("bs", JoinType.LEFT);
list.add(cb.equal(join.get("c").get("id"), id));

七婞 2022-09-09 23:37:05

@Override

public Page<Customer> findByRecycleCustomerList(int[] users, int taskId, int status, Customer customer, Pageable pageable) {
    return customerRepository.findAll((root, query, cb) -> {
        List<Predicate> predicate = new ArrayList<>();

        Join<Customer, Task> taskJoin = root.join("task", JoinType.INNER);
        Join<Customer, CallLog> callLogJoin = root.join("callLogs", JoinType.LEFT);
        callLogJoin.on(
                cb.between(callLogJoin.get("createDate"), taskJoin.get("startDate"),taskJoin.get("endDate")),
                cb.equal(callLogJoin.get("user").get("id"), root.get("user").get("id")),
                cb.equal(callLogJoin.get("task").get("id"), root.get("task").get("id"))
        );

        predicate.add(cb.equal(root.get("task").get("id"), taskId));
        if (users != null && users.length > 0) {
            CriteriaBuilder.In<Object> in = cb.in(root.get("user").get("id"));
            for (Integer id : users) {
                in.value(id);
            }
            predicate.add(in);
        }
        if (status != 0){
            predicate.add(cb.equal(callLogJoin.get("status"), status));
        }
        if (customer.getName() != null && customer.getName().length() > 0){
            predicate.add(cb.like(root.get("name"), "%"+ customer.getName() +"%"));
        }
        if (customer.getId() != null){
            predicate.add(cb.equal(root.get("id"), customer.getId()));
        }
        Predicate[] pre = new Predicate[predicate.size()];
        return query.distinct(true)
                .where(cb.and(predicate.toArray(pre))).getRestriction();
    }, pageable);
}
向地狱狂奔 2022-09-09 23:37:05

cb.eq(root.get("two").get("privilege"), root.get("one_privilege"));

在One实体中 添加
@OneToOne
private Two two;

吻泪 2022-09-09 23:37:05

你好!我也遇到这个问题了,咋写的啊

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文