Skip to content

Invalid SQL generated when non-root entity in aggregate has a collection #1734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamoamo opened this issue Feb 5, 2024 · 3 comments
Closed
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@jamoamo
Copy link

jamoamo commented Feb 5, 2024

If I have a nested entity aggregate where somewhere in the nest (not the root) there is a one-to-one relationship with a nested one-to-many relationship, the query from the one-to-many relationship entity will reference the incorrect column name. That sounds vague, even to me, so let me illustrate.

If I have a database structure such as below (imagine foreign keys from c -> b and b -> c):

CREATE TABLE a (a_id INT)
CREATE TABLE b (b_id INT, a INT)
CREATE TABLE c (c_id INT, b INT)

and entities for each table as follows (extraneous code excluded) where A contains a one-to-one relationship with B and B contains a one-to-many relationship with C.

public class C {
   @Id
   private int cId;
}

public class B {
   @Id
   private int bId;
   private Set<C> cChild;
}
public class A {
   @Id
   private int aId;
   private B bChild;
}

The above will result in an exception when querying the root of the aggregate using a repository bean along the lines of

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT "C"."C_ID" AS "C_ID" FROM "C" WHERE "C"."A_ID" = ?]

The generated SQL makes use of a column which does not exist in the table 'c' but only exists in table 'a'.

A workaround is to make the relationship between A and B to be a one-to-many relationship.

There is a minimalist project that reproduces the bug at https://github.com/jamoamo/spring-data-bug-reproducer

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 5, 2024
@ljpeters
Copy link

This looks similar to my reported issue #1739

It took some time for me to get redirected to spring-data-relational, so I'm just looking at already reported issues now.

@schauder
Copy link
Contributor

There is a snapshot available that fixes #1692
3.3.0-1692-collection-in-embedded-SNAPSHOT
And a PR to go with it: #1773

I assume this issue will be fixed by it as well.
If nobody objects I'll close this issue as a duplicate once the PR is merged.

@schauder schauder added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 18, 2024
@schauder
Copy link
Contributor

Duplicate of #1692

@schauder schauder marked this as a duplicate of #1692 Apr 18, 2024
@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants