Is there a way to define a post transaction hook instead of using the standard @PostPersist/Update/Delete but to keep the event like created/updated/deleted?
I’m using a redis (don’t know if should mention it now after the license change, but I have done it now ) and would like to update its content only when the transaction has been completed, when all the cascaded data has been update/created/deleted.
P.S. I do know that Hibernate search has something similar for their indexing needs with elastic/opensearch.
So what I needed wasn’t that hard but and was the JTA transaction handeling, but I had a hard time finding an implementation reference for how it works.
- First i needed to create a jakarta event
private jakarta.enterprise.event.Event<se.agreedSkiing.example.Entity1> dbLogEvent;
- Then fire said event from a
@PostPersist/PrePersist
@jakarta.persistence.PostRemove
public void sendEvents(se.agreedSkiing.example.Entity1 entity) {
dbLogEvent.fire(entity);
}
- Create an observer method with the correct transaction phase and wait for a db event to fire.
void afterSuccessfulTransactionObservingEntity1LogAsJson(
@jakarta.enterprise.event.Observes(during = jakarta.enterprise.event.TransactionPhase.AFTER_SUCCESS) Entity1 dto
) {
try {
var json = new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(dto);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}