spring-projects / spring-projects/spring-data-relational

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

Open
#2,112 0 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Aug 18, 2025.

type: enhancement
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.