在实体类的字段上的谓词,该类型是父母的类型,并在其孩子的字段上过滤

发布于 2025-01-22 07:07:27 字数 1645 浏览 2 评论 0原文

在实体A类中,有一个名为p的P1类型的字段。 P2和P3类从P1延伸。在谓词中,我想根据P2类的字段过滤结果。我该怎么做?

public class A {
    private P1 p;
}

public class P1 {
}

public class P2 extends P1 {
    private String s;
}

public class P3 extends P1 {
    private String u;
}

public interface ARepository extends CrudRepository<A, Long>, QuerydslPredicateExecutor<A>, QuerydslBinderCustomizer<QA> {
    @Override
    default void customize(QuerydslBindings bindings, QA a) {
        bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
    }
    default Page<A> findByFilter(AFilter filter, Pageable pageable) {
        Predicate predicate = new APredicatesBuilder().build(filter);
        return findAll(predicate, pageable);
    }
}

class APredicatesBuilder {
    private List<BooleanExpression> predicates = new ArrayList<>();
    private addPredicate(BooleanExpression predicate) {
        if (null != predicate) {
            predicates.add(predicate);
        }
    }
    BooleanExpression build(AFilter filter) {
        QA a = QA.a;
        if (filter.getS() != null) {
            addPredicate(a.p.s.eq(filter.getS()); // This is not possible. How to do it?
        }
        BooleanExpression result = null;
        if (!predicates.isEmpty()) {
            result = predicates.get(0);
            for (int i = 1; i < predicates.size(); i++) {
                result = result.and(predicates.get(i));
            }
        }
        return result;
    }
}

class AFilter {
    private String s;
}

正如我在代码中评论的那样,这是不可能在A上添加谓词并根据P2类中的字段S过滤结果。

In the entity class A there is a field of type P1 named as p. Class P2 and P3 is extended from P1. In the predicate I want to filter the result based on a field of class P2. How can I do this?

public class A {
    private P1 p;
}

public class P1 {
}

public class P2 extends P1 {
    private String s;
}

public class P3 extends P1 {
    private String u;
}

public interface ARepository extends CrudRepository<A, Long>, QuerydslPredicateExecutor<A>, QuerydslBinderCustomizer<QA> {
    @Override
    default void customize(QuerydslBindings bindings, QA a) {
        bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
    }
    default Page<A> findByFilter(AFilter filter, Pageable pageable) {
        Predicate predicate = new APredicatesBuilder().build(filter);
        return findAll(predicate, pageable);
    }
}

class APredicatesBuilder {
    private List<BooleanExpression> predicates = new ArrayList<>();
    private addPredicate(BooleanExpression predicate) {
        if (null != predicate) {
            predicates.add(predicate);
        }
    }
    BooleanExpression build(AFilter filter) {
        QA a = QA.a;
        if (filter.getS() != null) {
            addPredicate(a.p.s.eq(filter.getS()); // This is not possible. How to do it?
        }
        BooleanExpression result = null;
        if (!predicates.isEmpty()) {
            result = predicates.get(0);
            for (int i = 1; i < predicates.size(); i++) {
                result = result.and(predicates.get(i));
            }
        }
        return result;
    }
}

class AFilter {
    private String s;
}

As I commented in the code, this is not possible to add predicate on A and filter the results based on field s in class P2.

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

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

发布评论

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

评论(1

甜嗑 2025-01-29 07:07:27

这是您的类层次结构的问题:a具有字段p type p1的p ,但是字段s确实仅在类p2中存在。我将推荐迁移p键入p2 s移动到父级 p1 p1 代码>。

最后,这与QueryDSL无关,并且与纯Java一样发生。

This is an issue with your class hierarchy: A has a field p of type P1 but the field s does only exist in class P2. I would recomment to migrate p to type P2 or to move s to parent class P1.

At the end, this has nothing to do with querydsl and happen in the same way with pure java.

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