Closed
Description
I've created the sample repo to demonstrate the problem. Run the following test:
In a nutshell, assume we have an aggregate:
@Table
public class Category {
@Id
private Long id;
private String name;
@MappedCollection(idColumn = "category_id", keyColumn = "category_id")
private List<ProductCategories> productCategories;
}
@Table
public class Product implements Persistable<Long> {
@Id
private Long id;
private String name;
@MappedCollection(keyColumn = "product_id", idColumn = "product_id")
private List<ProductCategories> productCategories;
}
@Table
public class ProductCategories {
private AggregateReference<Product, Long> productId;
private AggregateReference<Category, Long> categoryId;
}
And when running the following code, the loaded foundProcdut
does not contain the ProductCategories
:
Product product = Product.createNew(
1L,
"product first",
List.of(
new ProductCategories()
.setProductId(AggregateReference.to(1L))
.setCategoryId(AggregateReference.to(1L))
)
);
// when.
Product saved = productRepository.save(product);
// then.
Optional<Product> foundProduct = productRepository.findById(saved.getId());
The reason it does not contain ProductCategories
is because the INSERT, generated into ProductCategories
, has product_id
set to 0
, instead of 1
.
Note: It does not even matter is the ID generated on the client side or as an IDENTITY column on the database side. The problem remains.