Reputation: 94
Is there any way to intercept or change the document
before MongoRepository.save(e)
updates it?
I am trying to push a subproperty inside and array in a document. I have tried to manipulate DBObjects
by implementing converter
(using custom converter) but the $push
operation did not work over there.
I think to make it work I have to implement something like mongoOperation.update(dbObjectMatch,dbObjectUdate).
I found MongoRepositorysave(document)
doesn't support partial update, i.e write only the change to an existing document. I want to know the internal code of MongoRepository.save
to override the default behavior.
I have implemented MyRepositoryCustom
where I can override save
by extending the same in MyRepository
, which extends MongoRepository<T, ID extends Serializable>
and then used mongoTemplate.updateFirst(query,update,Clazz.class)
to achieve what I am looking for but I am not satisfied.
Upvotes: 4
Views: 6336
Reputation: 842
You can fragmentally override the MongoRepository by ensuring you match the signature of method in the custom interface.
E.g. If you want to override save()
method, create a new interface like
interface CustomizedSave<T> {
<S extends T> S save(S entity);
}
Implement this interface:
@Component
class CustomizedSaveImpl<T> implements CustomizedSave<T> {
//This is optional, I added it to just show you can use it here
MongoTemplate mongoTemplate;
@Autowired
public CustomizedSaveImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
public <S extends T> S save(S entity) {
// Your custom implementation
}
}
Extend the interface in the base MongoRepository like
interface UserRepository extends CrudRepository<User, Long>, CustomizedSave<User> {
}
This will ensure only save menthod gets overridden and rest stay as is.
Upvotes: 0
Reputation: 81862
You have multiple, slightly different questions:
From your title:
Change/Override Default Behaviour of Mongorepository Save() ( S save(S var1)) Method
You can use custom implementations to override the behavior of existing methods in Spring Data repositories. See the reference documentation how to do that. Your last paragraph suggests you already do that. Unfortunately you don't tell us why you aren't satisfied with this.
Is there any way to intercept/change before Mongorepository Save() ( S save(S var1)) method For document update.
Yes a Spring Data MongoDB repository fires various life cycle events for this purpose. Once again, see the reference documentation for details.
I want to know the Internal code of Mongorepository Save
What you are looking for is SimpleMongoRepository.java
which delegates for almost all work to MongoTemplate.java
Upvotes: 8
Reputation: 18119
You're looking for Lifecycle Events.
Overriding repository base methods allows you to interact with the domain object itself but the mapping happens inside of MappingMongoConverter
.
Saving an object will fire events such as
These events carry a reference to your saved object. AfterConvertEvent
additionally exposes the mapped representation (DBObject
) of your object which you can change/enhance.
You can listen to these events by configuring a listener bean such as ApplicationListener<AfterConvertEvent>
.
Upvotes: 3