1

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.

Mark Rotteveel
110k239 gold badges160 silver badges232 bronze badges
asked Feb 8 at 9:55

1 Answer 1

0

This is currently a limitation of Spring Data JDBC. Feel free to create a ticket for an enhancement.

answered Feb 10 at 6:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.