Skip to content

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

@chrshnv

Description

@chrshnv

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 (?)

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions