What you describe sounds like a bug to me.
If you set the id in an event listener it should still be handled as a new instance.
Please file an issue at https://jira.spring.io/browse/DATAJDBC
How to design an implementation that can be saved with @CreatedDate and @CreatedBy when using the first save? @LastModifiedDate and @LastModifyBy?
As a work around you could combine the IsNewAwareAuditingHandler
with your event handler for setting the id.
What is the difference between an aggregate root and an entity?
An entity is an object that is identified by its id, note that the id might be implicit.
See below.
An aggregate is a (typically small) cluster of objects that belong together and get persisted in a single transaction.
For example a PurchaseOrder
and it's LineItem
are both entities that belong to the same aggregate.
It is perfectly possible for a single object to be it's own aggregate.
The aggregate root is one entity from an aggregate.
All interactions with the aggregate members should go through the aggregate root.
This allows the aggregate root to control consistency.
For example in the example given above PurchaseOrder
would be the aggregate root.
You therefore would not have a getItems()
getter that returns a life collection of the items. Instead you would have maybe addItem(productId, amount)
and getItems()
would return a copy of the items, so changing those won't affect the aggregate.
Martin Fowlers definition: https://www.martinfowler.com/bliki/DDD_Aggregate.html
Great articles about aggregates by Vaughn Vernon:
https://dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_1.pdf
https://dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_2.pdf
https://dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_3.pdf