Yes, you must implement a custom method on the repository and your push method would be something like this :
public class FooRepositoryImpl implements
AppointmentWarehouseRepositoryCustom {
@Autowired
protected MongoTemplate mongoTemplate;
public void pushMethod(String objectId, Object... events) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("id").is(objectId)),
new Update().pushAll("events", events), Foo.class);
}
}
You can do this but I ran into an issue where the "_class" field wasn't being preserved.
The pushed object itself was run through the configured converter but for some reason the "_class" field of that pushed object wasn't written.
However, if I injected the converter and wrote the object to a DBObject myself, then the "_class" field was preserved and written. The thus becomes:
public class FooRepositoryImpl implements
AppointmentWarehouseRepositoryCustom {
@Autowired
protected MongoTemplate mongoTemplate;
public void pushMethod(String objectId, Object event) {
DBObject eventObj = new BasicDBObject();
converter.write(event, eventObj);
mongoTemplate.updateFirst(
Query.query(Criteria.where("id").is(objectId)),
new Update().push("events", eventObj), Foo.class);
}
}
No comments:
Post a Comment