spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.
https://spring.io/projects/spring-data-jdbc
Apache License 2.0
753 stars 345 forks source link

SQL column name resolution failed for embeddable with `AggregateReference` #1695

Closed jipipi closed 8 months ago

jipipi commented 9 months ago

The sql query is not generate with the right column name for embedded AggregateReference property. The embedded prefix definition is not set.

Example :

public record Parent(
        @Id Long id,
        String name,
        @Embedded.Nullable(prefix = "nested_")
        Nested nested
){}

public record Nested(
        AggregateReference<Child,Long>  child_id, // ko when generate sql query (select)
        String type
){}

public record Child(
   @Id Long id,
   String name) {}
CREATE TABLE IF NOT EXISTS Parent (
    id INTEGER IDENTITY PRIMARY KEY,
    name VARCHAR(100),
    nested_child_id NUMERIC,
    nested_type VARCHAR(100)
);

CREATE TABLE IF NOT EXISTS Child (
                                   id INTEGER IDENTITY PRIMARY KEY,
                                   name VARCHAR(100)
);

A simple loading of parent will generate the issue (the save is ok) :

 var parent = repository.findById(1L)//failed because sql query is not correct the embedded prefix is missing: "PARENT"."CHILD_ID" must be "PARENT"."NESTED_CHILD_ID"

SQL generated : SELECT "PARENT"."ID" AS "ID", "PARENT"."NAME" AS "NAME", "PARENT"."NESTED_TYPE" AS "NESTED_TYPE", "PARENT"."_CHILDID" AS "CHILD_ID" FROM "PARENT" WHERE "PARENT"."ID" = ?

Here project to reproduce error : Test project