Skip to content
Advertisement

Query on distant/not directly related entity

I’m trying to create a query with a where clause on a not directly related table.

My table/entity structure looks like this:

I have got an entity Entity with a ManyToOne relation to Relation. Relation has a ManyToMany relation to DistantRelation.

I have a JpaSpecificationExecutor<Entity> on which I call findAll() with a Specification<Entity>.

How do I setup my entity and/or my specification so I can filter on one of the fields of DistantRelation?

Advertisement

Answer

Entities definition:

JavaScript

Solution 1. Subquery with optimal joins

JavaScript

Generated query:

JavaScript

Solution 2. Subquery with all relation joins

JavaScript

Generated query:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement