1

I have seen the response in the previous question! , but it did not solve my problem.

I traced the code of spring-data-jdbc and found that as long as the BeforeSaveEvent event is customized and a custom ID is set in this event, after the custom event is executed, it continues to trigger the execution of RelationalAuditingEventListener#onApplicationEvent on the entity that has been set to ID. The isNew decision is made, ieNew=false.

// IsNewAwareAuditingHandler#markAudited // Triggers the markModified method. entity.isNew(object) ? markCreated(object) : markModified(object);

What is the difference between an aggregate root and an entity? How to design an implementation that can be saved with @CreatedDate and @CreatedBy when using the first save? @LastModifiedDate and @LastModifyBy?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Hyman Yang
  • 33
  • 6
  • @Jens Schauder Thank you very much! I can't connect to `https://jira.spring.io/browse/DATAJDBC` because of geographical network restrictions. I created a demo on github that can reproduce the problem just mentioned. [spring-data-jdbc-demo](https://github.com/althyman/spring-data-jdbc-demo.git) I feel like a bug... or my understanding of the use of auditing is wrong. – Hyman Yang Jul 08 '19 at 13:11
  • Bummer. Why would they block an issue tracker! Thanks for the reproducer. I created and issue for it: https://jira.spring.io/browse/DATAJDBC-390 – Jens Schauder Jul 08 '19 at 13:48
  • May I ask where https://jira.spring.io/browse/DATAJDBC is blocked? – Jens Schauder Jul 08 '19 at 16:24
  • Intermittently unable to open, most of the time the state is continuous loading... I am in China... In between Beijing time 19:00 and 0:00, can open a single page, and then click on other links in the page will continue to load The time to open the home page is about 45 seconds... – Hyman Yang Jul 09 '19 at 06:53

1 Answers1

1

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

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348