Skip to content

FeatureRequest: Publish Events registered on result of entitymanager #3388

@thuri

Description

@thuri

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 ❤️

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions