Can assign Child for Parent in Query DSL?

Viewed 202

there QParent, QChild1, and QChild2 I try to use QParent instead of each child.

the class that contains all predicates

class Filter {
        private QParent query;

        public Filter(QParent query) {
            this.query = query;
        }

       BooleanExpression getPredicates(String name) {
          name == null ? null : query.name.eq(name);
          return query.isNotNull().and(name);
    }
}

then I send a child as an argument for the Filter constructor

  childOneRepo.findAll(new Filter(QChild1.child1).getPredicates("test name"));

but new Filter(QChild1.child1) give me an error.

although child1 and child2 extend the parent class.

1 Answers

While the entities extend each other, the query types do not. Instead, you ahve to use new Filter(QChild1.child1._super).

Related