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

@CreatedDate doesn't work on one-to-many relationship #1607

Closed
livanov opened this issue Sep 11, 2023 · 4 comments
Closed

@CreatedDate doesn't work on one-to-many relationship #1607

livanov opened this issue Sep 11, 2023 · 4 comments
Assignees
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged

Comments

@livanov
Copy link

livanov commented Sep 11, 2023

A reproducible example here.

When we save the Aggregate Root, the @CreatedDate annotated field is correctly set and saved on the aggregate root and on the one-to-one entity relationship. However, it fails to assign it on the one-to-many entity relationship.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 11, 2023
@schauder schauder changed the title @EnableJdbcAuditing doesn't work on one-to-many relationship @CreatedDate doesn't work on one-to-many relationship Sep 11, 2023
@schauder
Copy link
Contributor

Auditing columns are currently by design only supported for the aggregate root, since based on DDD there really is only one life cycle: that of the aggregate.

Supporting auditing columns on inner entities of an aggregate would also be difficult since there really isn't a easy way of telling if those are new or modified without accessing the database for this.

Leaving this open in order to have a short team discussion about it.

@schauder schauder added the for: team-attention An issue we need to discuss as a team to make progress label Sep 11, 2023
@livanov
Copy link
Author

livanov commented Sep 11, 2023

Thanks for the prompt reply!

Auditing columns are currently by design only supported for the aggregate root, since based on DDD there really is only one life cycle: that of the aggregate.

I was also thinking that this could be why it is not supported. However, I think there is a valid business requirement that we'd like to see who created what within the aggregate - in my case - who changed the address. When I think about it now, I'd like to differentiate between who touched the Address and who touched the Person details, whereas even if inner entities' auditing was supported, I guess that the aggregate would still be updated.

Supporting auditing columns on inner entities of an aggregate would also be difficult since there really isn't a easy way of telling if those are new or modified without accessing the database for this.

I guess that the reason it works for the one-to-one relationship is some form of a side effect and not by design?

For the sake of anyone else looking of a workaround to that. I've implemented a custom BeforeConvertCallback<Object> that runs after org.springframework.data.relational.auditing.RelationalAuditingCallback and traverses the object with reflection looking for nested entities that "need" auditing. I am not sure how I can disable the org.springframework.data.relational.auditing.RelationalAuditingCallback and completely replace it.

@schauder
Copy link
Contributor

I guess that the reason it works for the one-to-one relationship is some form of a side effect and not by design?

Strictly speaking it shouldn't work. Making it work could be considered a mistake. Although probably many wouldn't like me changing that now ...

@schauder
Copy link
Contributor

schauder commented Sep 12, 2023

We discussed this internally.

TL;DR: We'll leave it as it is.

If we support auditing annotations on inner entities they really should get the same value, the aggregate root.
This is ok for single references (embedded or not) to keep the auditing data somewhat seperate.
But for multivalued references (Set, List, Map) this would be quite useless.
So we keep it as it is: supported for single references but not for multivalued ones.

For the sake of anyone else looking of a workaround to that. I've implemented a custom BeforeConvertCallback that runs after org.springframework.data.relational.auditing.RelationalAuditingCallback and traverses the object with reflection looking for nested entities that "need" auditing.

You might find it helpful to use the JdbcMappingContext and its methods providing PersistentEntity and PersistentPropertyPath.

I am not sure how I can disable the org.springframework.data.relational.auditing.RelationalAuditingCallback and completely replace it.

Just don't enable auditing.

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants