question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aggregate root is saved but domain events is not published, if uses transaction and do not explicitly call "save" [DATAJPA-1412]

See original GitHub issue

teclxl opened DATAJPA-1412 and commented

Issue

If it uses @DomainEvent for publishing domain events, it only executes publishing when the method like “CrudRepository#save*” was calling.This is perfect for persistence-oriented repository, but not for collection-oriented repository, because of we usually don’t call “save” when using collection-oriented repository.

Unfortunately, JPA & Hibernate is collection-oriented.

Possible solution

I found a blog that mentions the issue. It provides solution, but I don’t know if it works. The blog is:

https://blog.pchudzik.com/201710/domain-events/

Another question for @DomainEvents

How can I use @DomainEvents, if I just want to publish event ,ant don’t want to persist the aggregate root. A detailed description of the questionis here: https://stackoverflow.com/questions/51998921

My Test Code

version is :2.0.7

    // Method in aggregate root
    public void demoOne() {
        registerEvent(new StudentDemoOneEvent(id));
    }


    // Listener
    @TransactionalEventListener
    public void demoOne(StudentDemoOneEvent event) {
        log.info("DemoOne listener executed! Student id is: " + event.getStudentId().getUuid());
        dummyService.exe();
    }


    // Junit Test
    @Test
    @Transactional
    public final void testDemoOne_allSuccess() {
        String newName = "testDemoOne_allSuccess";
        TestTransaction.flagForCommit();

        StudentAtDb student = repo.findById(STUDENT_ID).get();
        student.setName(newName);
        student.demoOne();
        // repo.save(student);
        // if call save, saved and listenered. if not call, saved but not listenered.
        TestTransaction.end();

        assertEquals(repo.findById(STUDENT_ID).get().getName(), newName);
        assertThat(capture.toString(), containsString("DemoOne listener executed!"));
    }


Affects: 2.0.9 (Kay SR9)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
spring-projects-issuescommented, Dec 30, 2020

teclxl commented

Jens SchauderI am very sorry to did not pay attention to this issue later.

My current solution is to force that the save method must be explicitly called at the end of the service. I used a Hibernate interceptor that was inserted into the preFlush, which throws an exception when it finds that the aggregate has some domain events.

I found that the issue has become resolved. Can I ask if it is solved or not a problem?

Also, I can’t seem to close the issue, please close it

0reactions
spring-projects-issuescommented, Dec 30, 2020

Jens Schauder commented

Thanks for letting us know about the solution you ended up using teclxl. I like the idea of checking in a listener if an aggregate has still events stored

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deal with the aggregate root without repository, when ...
I plan to refactor the way of publishing domain events to using Spring Data Common. I can design an aggregate root named ChannelToClose...
Read more >
Domain events: Design and implementation | Microsoft Learn
NET Applications | Get an in-depth view of domain events, a key concept to establish communication between aggregates.
Read more >
How to publish and handle Domain Events - Kamil Grzybek
In my opinion it is good approach to always handle domain events in existing transaction and treat aggregate method execution and handlers ...
Read more >
DDD – Aggregate Roots and Domain Events publication
A Domain Event represents something that has happened in the domain. ... of publishing the registered events only if the save transaction ......
Read more >
Don't publish Domain Events, return them! - Jayway
But more importantly, what is the programming model for publishing events? I think this issue highlights how object oriented programmers are so ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found