'JPA: How to save from flux in reactive repository
Given a repository that extends ReactiveCrudRepository:
interface PersonRepo extends ReactiveCrudRepository<Person, Long> {
// ...
}
and a Person data class with Name and Age
class Person { ... }
Should this client code not save my persons to the repository?
private void savePeople() {
Person p1 = new Person(23L, "p1", 22);
Person p2 = new Person(25L, "p2", 39);
List<Person> peoples = new ArrayList<>();
peoples.add(p1);
peoples.add(p2);
personRepository.saveAll(Flux.fromIterable(peoples));
}
I can't find p1 or p2 in the H2 in-memory database. Why not?
Solution 1:[1]
change you method signature to private Mono<Void> savePeople()
and then replace personRepository.saveAll(Flux.fromIterable(peoples)) with following
return personRepository.saveAll(Flux.fromIterable(peoples)).then();
last statement is not going to be invoked until you subscribe to saveAll method returned Flux. follow above steps. you will see p1 & p2 in DB
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jbaddam17 |
