Skip to content

The columns of the child aggregates are rendered incorrectly in the Criteria API #2112

@mipo256

Description

@mipo256

I've created a MRE for this.

The problem is, that the Criteria queries for the nested child aggregate (one-to-one in the particular case above) seems to always qualify the.

For instance, given the model:

@Table
public class RootAgg {

    @Id
    private Long id;

    @Column(value = "name_c")
    private String name;

    @MappedCollection(idColumn = "root_agg_id")
    private ChildAgg child;

    @Table
    public static class ChildAgg {

        @Id
        private Long id;

        private String type;
    }

For the following criteria query:

    @Test
    @Sql(statements = """
            CREATE TABLE IF NOT EXISTS root_agg(id bigserial, name_c text);
            CREATE TABLE IF NOT EXISTS child_agg(id bigserial, type text, root_agg_id bigint);
            """)
    void testQueryingByChildAgo() {
        RootAgg rootAgg = new RootAgg()
                .setName("my_name")
                .setChild(new ChildAgg().setType("type"));

        List<RootAgg> result = jdbcAggregateTemplate.findAll(
                Query.query(Criteria
                        .where("name_c")
                        .is("my_name")
                        .and(Criteria.where("child.type").is("type"))),
                RootAgg.class
        );

        Assertions.assertThat(rootAgg).isNotNull();
    }

This is the SQL select that is generated:

SELECT "root_agg"."id"     AS "id",
       "root_agg"."name_c" AS "name_c",
       "child"."id"        AS "child_id",
       "child"."type"      AS "child_type"
FROM   "root_agg"
       LEFT OUTER JOIN "child_agg" "child"
                    ON "child"."root_agg_id" = "root_agg"."id"
WHERE  "root_agg".name_c = ?
       AND ( "root_agg"."type" = ? ) -- This is the problem

And it happens with every column that exists in the ChildAgg. It seems that there is a problem in the child aggregate columns qualification.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions