在实体类的字段上的谓词,该类型是父母的类型,并在其孩子的字段上过滤
在实体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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您的类层次结构的问题:
a
具有字段p
typep1
的p ,但是字段s
确实仅在类p2
中存在。我将推荐迁移p
键入p2
或将s
移动到父级 p1 p1 代码>。最后,这与QueryDSL无关,并且与纯Java一样发生。
This is an issue with your class hierarchy:
A
has a fieldp
of typeP1
but the fields
does only exist in classP2
. I would recomment to migratep
to typeP2
or to moves
to parent classP1
.At the end, this has nothing to do with querydsl and happen in the same way with pure java.