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

@EnableMongoAuditing does not play well with MongoRepository#insert #4902

Closed
rfelgent opened this issue Feb 19, 2025 · 3 comments
Closed

@EnableMongoAuditing does not play well with MongoRepository#insert #4902

rfelgent opened this issue Feb 19, 2025 · 3 comments
Assignees
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@rfelgent
Copy link

rfelgent commented Feb 19, 2025

Hello,

when pre-populating the @id field before doing a MongoRepository#insert, the auditing functionality will only set the @LastModifiedBy field and ignore the @CreatedBy field.

I expect the MongoRepository#insert method to force a isNew=true flag/strategy throughout the whole persist attempt so that the setting EnableMongoAuditing#modifyOnCreate() default true can kick in.

My current fix is to add a custom EntityCallback ensuring the setting of CreatedBy. This EntityCallback is getting executed after org.springframework.data.mongodb.core.mapping.eventAuditingEntityCallback

@rfelgent rfelgent changed the title [bug] @EnableMongoAuditing#modifyOnCreate does not play well with MongoRepository<T, ID>#insert [bug] @EnableMongoAuditing does not play well with MongoRepository#insert Feb 19, 2025
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 19, 2025
@mp911de
Copy link
Member

mp911de commented Feb 20, 2025

This is due to a mix of intentions vs. is-new detection. While one could argue that exposing the insert method on the repository is a root of that problem, in reality we have also an insert method on MongoTemplate that uses the same entity callback.

Ideally, the repository should determine itself whether an entity is new or whether it exists. However, we do not have any means of propagating the is-new aspect into callbacks and I doubt we will ever have because that contextual information is not part of the interface.

@mp911de mp911de self-assigned this Feb 24, 2025
@mp911de mp911de changed the title [bug] @EnableMongoAuditing does not play well with MongoRepository#insert @EnableMongoAuditing does not play well with MongoRepository#insert Feb 24, 2025
@mp911de mp911de added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 24, 2025
@mp911de
Copy link
Member

mp911de commented Feb 24, 2025

The two only options you have are:

  1. Implementing Persistable interface to indicate to the auditing listener whether the object should be considered new.
  2. Using optimistic locking via @Version. Optimistic locking checks the version property to determine whether an object is new or should exist on the database.

@mp911de mp911de closed this as completed Feb 24, 2025
@rfelgent
Copy link
Author

rfelgent commented Feb 27, 2025

Hi @mp911de ,

thx for your time and your suggestions.
Please allow me to give feedback to your suggestions:

  1. I was afraid, that you would come up with the Persistable interface. I dropped it entirely in my current project in favor of the "jdbcAggregationOperation#insert()" approach.

There are scenarios where "the isNew()" logic just fits better in business/service code instead of entity/model code.

I work with a lot of generated (model) code - extending them with logic (or an interface) is sometimes easy work and sometimes hard work.

  1. Interesting ! If I am not mistaken, using @versioning functionality would "circumveil" my problem - how come ? Because of "version=0"/"version=null" ? The "id" field would not be taken into account but rather the "version" field ?

I like your 2) suggestion! Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

3 participants