简体   繁体   中英

Auditing with spring-data-jdbc?

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM