I have table A that has an attribute that is an entity in table B that in turn is connected to a table C.
The (working) SQL query looks like this:
SELECT a.* from A a
LEFT JOIN B b ON a.b_id=b.id
LEFT JOIN C c ON b.c_id=c.id where c.attribute=VALUE;
Basically the VALUE is what Im filtering on. There is a one-to-one relationship from A->B and a one-to-one relationship from B->C.
There are other parameters Im also filtering on so I have a Specification
class that generates a Predicate
for each parameter that is passed in to build a list of predicates which is then ANDed together at the end. The Specification
is being called from my Repository
using something like findAll(MySpecificationClass.search(params))
.
Im having a hard time understanding how to write this SQL query using CriteriaBuilder
inside my Specification
class.
NOTES:
- This is in a Spring Boot application written in Kotlin.
- My specification class is a singleton so I can't autowire
EntityManager
for example. - I tried to use a chain of
get()
s to navigate from A, through B to C but that doesn't work - it seems to return all records regardless. - My project is using spring-data-jpa 2.2.6