ReactiveMongoRepository insert return converter

199 Views Asked by At

Insertion of an entity with an Instant value, using ReactiveMongoRepository:

MyEntity inserted = myReactiveMongoRepository.insert(entity).block();
System.out.println("inserted.getSent() = " + inserted.getSent());
// inserted.getSent() = 2022-09-17T00:20:58.399300383Z

Reloading the entity after insertion:

MyEntity loaded = myReactiveMongoRepository.findById(inserted.getId()).block();
System.out.println("loaded.getSent() = " + loaded.getSent());
// loaded.getSent() = 2022-09-17T00:20:58.399Z

The instant value has been trimmed to milliseconds precision in Mongo, which is fine. Still, the insert above returns an object containing the original value.

How to return the converted instant right away insertion?

1

There are 1 best solutions below

1
Jens Schauder On BEST ANSWER

You can chain the calls.

MyEntity insertedAndReloaded = myReactiveMongoRepository.insert(entity)
        .flatMap(inserted -> myReactiveMongoRepository.findById(inserted.getId())
        .block(); // don't block in production.

But there isn't any special support for reloading entities automatically in order to obtain generated data from the database.