1

I have an Example entity class that extends PanacheEntityBase. My goal is to get a stream or list of examples and then run updates on each one and persist those updates, then run another function that does some other stuff - I know this other function works and it takes in an id parameter and returns a Uni<Void>.

A very similar function is working that takes an id, then uses Example.findById(id) to update a single Example record. However, I cannot figure out how to get this working with multiple records.

Here is the code that does work for a single Example record, taking in the id as a parameter:

return Panache.withTransaction(() -> Example.<Example>findById(id)
    .onItem().ifNull().failWith(new NotFoundException("Example not found."))
    .onItem().transformToUni(example -> {
        example.setState("new state");
        example.setStateChangedAt(Instant.now());
        return example.<Example>persist();
    })
    .onItem().call(example -> anotherService.anotherFunction(example.getId()))
    .onItem().ignore()
    .andContinueWithNull()
);

So for multiple records, I am trying a similar thing (it should run on every Example record where state="old state").

Right now, after the .transform(...).onItem() it looks like it is working with a Uni<Example> where as in the above single-record code, at that point it is working with just an Example and I do not understand why.

return Panache.withTransaction(() -> Example.<Example>stream("state", "old state")
    .onItem().transform(example -> {
        example.setState("new state");
        example.setStateChangedAt(Instant.now());
        return example.<Example>persist();
    }).onItem().call(example -> {
        anotherService.anotherFunction(example.getId());
    })
    .onItem().ignore()
    .andContinueWithNull()
);

I have also looked into some options using Example.list() instead of Example.stream(), but I have been unable to get it working that why either. I have been struggling through all this Uni and Multi stuff and still do not have a super firm grasp on it. Please assist in getting that second update transaction working.

Kyle Hobbs
  • 445
  • 1
  • 4
  • 14

0 Answers0