Using Spring Data Relational (Spring Data JDBC V3.3.6), I have an entity setup similar to the following:
@Table("entity_a")
public record EntityA(
@Id
Long id,
@Column("entity_a_id")
EntityB other) {
}
@Table("entity_b")
public record EntityB(
Long entityAId,
String value) {
}
Creation & updating works like a charm, but I faced some limitations trying to query using JdbcAggregateTemplate. I would like to get all entities, where EntityB has a certain value.
Utilizing the Criteria API as below is not working out:
jdbcAggregateTemplate.findAll(query, EntityA.class);
No variation of a defined Criteria leads to the expected SQL query generation. The generator always replaces the table name (or alias) of the joined entity by the parent entity.
The resulting exception shows that the generated query always looks as follows:
SELECT ...
FROM "entity_a"
LEFT OUTER JOIN "entity_b" "other" ON "other"."entity_a_id" = "entity_a"."id"
WHERE ("entity_a"."value" = ?)
I tried the following variations
query = query(Criteria.where("other_value").is("something"));
and
query = query(Criteria.where("other.value").is("something"));
Both caused the same result.
How can I archive the expected query:
SELECT ...
FROM "entity_a"
LEFT OUTER JOIN "entity_b" "other" ON "other"."entity_a_id" = "entity_a"."id"
WHERE ("other"."value" = ?)
Am I doing something wrong or is it just currently not possible using the Criteria API? Switching to Spring Data JPA is currently no option for me.
1 Answer 1
This is currently a limitation of Spring Data JDBC. Feel free to create a ticket for an enhancement.
Comments
Explore related questions
See similar questions with these tags.