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:
Incorrect SQL for
AggregateReferencewith composite id infindBy...InSummary
Derived query methods using
AggregateReferencewith 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
Actual SQL
Minimal example
Reproduction
Expected behavior
Spring Data JDBC should expand composite ids into:
instead of: