Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identifier does not correctly propagate from root to child aggregate on insert #2004

Open
mipo256 opened this issue Mar 1, 2025 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@mipo256
Copy link
Contributor

mipo256 commented Mar 1, 2025

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants