Mongotemplate.updateFirst() doesn't update @La

2020-06-27 09:31发布

问题:

I'm using annotation based configuration.

@Configuration
@Profile("default")
@ComponentScan(basePackages = "com.*")
@EnableMongoRepositories
@EnableMongoAuditing
public class ApplicationDataConfig ....

And I also have @LastModifiedDate, @CreatedDate annotation set on fields of type org.joda.time.DateTime. When I use

org.springframework.data.mongodb.repository.MongoRepository.save(entity)

both the audit fields are updated just fine. But when I use

org.springframework.data.mongodb.core.MongoTemplate.updateFirst()/updateMulti()

the lastupdated time is not being updated. Does anybody have a clue of what could be wrong? I'm using spring-data-mongodb1.8.3

回答1:

I found that the @Id field needed to be null at the time of save() only for the @CreatedDate and @CreatedBy annotations. The @LastModifiedDate and @LastModifiedBy fields worked regardless of whether the @Id field was initialized or not.

See here: https://stackoverflow.com/a/31018339/5939750