spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Publish domain events from result of persistence operation

Open thuri opened this issue 1 year ago • 3 comments

First of all I'm sorry if this is not the right place for this but the stackoverflow question related to this was not answered but after looking into the code i might have found a solution that I'd like to propose.

I will describe my use case first:

I want to use JPA event handlers like PrePersist etc. to register domain events for example domain events that inform potential event listeners about new domain objects that have been persisted.

So i add the following methods to the AggregateRoot:

    @PrePersist
    fun addCreatedEvent() {
        logger.info("Adding created event for $this")
        this.registerEvent(ExampleCreated(this))
    }

This works but only if the entity uses generated Ids, implements Persistable or the @Version annotation. But it doesn't work if the entity's Id is predefined by the calling code (e.g. initialized in the constructor). The "problem" is that the PrePersist is called on the copy of the object that has been created by the entity manager because the object is seen as DETACHED and TRANSIENT.

My question is whether it would be possible to change the EventPublishingMethodInterceptor#invoke method so that it will also call

eventMethod.publishEventsFrom(result, publisher);

in addition to the call of

eventMethod.publishEventsFrom(argument, publisher);

That should also triggere eventhandlers on domain events registered during the hibernate event listeners on the copy of the entity.

Thanks in advance and for your great work. I love the Domain events feature ❤️

thuri avatar Oct 23 '25 11:10 thuri

While we could certainly consider extracting events from the result object of the ultimate persistence, too, I wonder what the reason is that you attach the event registration to a persistence-technology-specific callback. Domain events are created in the course of domain operations. So I wonder if it makes more sense to attach that in a constructor?

odrotbohm avatar Oct 28 '25 14:10 odrotbohm

Hey thanks for looking into this.

To be honest I did not think about persistence-technology-specific things. Maybe because I was developing a application using JPA and was not looking over my own nose.

Registering the event in the constructor is a good idea. But I thought about a more generic approach which could be reusable and also support Deleted Domain Events or maybe Updated Events etc.

Using the jakarta.persistance annotations would allow to use a generic approach to create such events. With them I could create a generic EventListener that could be used with the @EntityListener annotation on any entity. I started a spring-data-jpa fork where i try to implement this.

Extracting the events from the result object may work to. This means they would be removed from the actually persisted object and registered on the "argument" instance, right? otherwise it would not be guaranteed that the events won't be published a second time. Right?

thuri avatar Oct 28 '25 18:10 thuri

After trying different approaches to my idea I was unable to find a good solution for using the JPA eventlisteners to reliably generate (lifecycle-)domain events. Especially for the most interesting case to be able to recognize that child entites have been removed. I think this is to the fact that the eventlisteners won't always be triggered when the repositories save or delete method is called. At least it's not guaranteed that the *Remove JPA-eventlisteners are executed on the save of a parent enttiy.

So I think it doesn't make sense anymore to change the current behaviour.

thuri avatar Nov 30 '25 17:11 thuri