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

Incorrect SQL for `AggregateReference` with composite id in `findBy...In`

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

@christophstrobl is already working on this.

Since Apr 10, 2026.

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

Description

Incorrect SQL for AggregateReference with composite id in findBy...In

Summary

Derived query methods using AggregateReference with a composite id generate incorrect SQL.

Instead of expanding the composite key into multiple columns, Spring Data JDBC treats it as a single column.


Expected SQL

SELECT "order_item"."external_id",
       "order_item"."sku",
       "order_item"."quantity"
FROM "order_item"
WHERE ("order_item"."external_id" = ? AND "order_item"."sku" = ?)
   OR ("order_item"."external_id" = ? AND "order_item"."sku" = ?)

Actual SQL

SELECT "order_item"."external_id",
       "order_item"."sku",
       "order_item"."quantity"
FROM "order_item"
WHERE "order_item"."product" IN (?, ?)

Minimal example

public record ProductId(
    @Column("external_id") UUID externalId,
    @Column("sku") String sku
) {}
@Table("order_item")
class OrderItem {

    private AggregateReference<Product, ProductId> product;

    @Column("quantity")
    private Long quantity;
}
interface OrderItemRepository extends ListCrudRepository<OrderItem, Object> {

    List<OrderItem> findByProductIn(
        Collection<AggregateReference<Product, ProductId>> products
    );
}

Reproduction

List<AggregateReference<Product, ProductId>> query =
    List.of(AggregateReference.to(productId));

orderItemRepository.findByProductIn(query);

Expected behavior

Spring Data JDBC should expand composite ids into:

(external_id = ? AND sku = ?)

instead of:

product IN (?)

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.