Spring querydsl predicate: relationship support

2020-07-24 04:27发布

问题:

I have a Client entity with an OneToOne relation with Contact. If I want to get the client name, I need to write:

 client.getContact().getLastName()

I use querydsl lib to filters client list in my spring boot app

 /api/clients?contact.lastName=Doe

My controller signature:

 public ResponseEntity<ResponseListDTO> getClients (
        @QuerydslPredicate(root = Client.class) Predicate predicateClient,
        Pageable pageable)

It works well: predicate is well created and filters works.

But now I need to filter on a field inside two relationship level:

 client.getContact().getAddress().getCity()

So I wan't to write:

 /api/clients?contact.address.city=XXX

But its not mapped with @QuerydslPredicate. No error but the @QuerydslPredicate predicate is still null (like if no filters).

Thanks!

回答1:

Found the solution with @QueryInit here: http://www.querydsl.com/static/querydsl/4.0.7/reference/html_single/#d0e2245