AfterCommitChanges: for deleted entity

I followed the example here in the guide with a little modification in order to trigger a process whether it is inserted or deleted.

@TransactionalEventListener
    public void onHolidayCalendarDayChangedAfterCommit(EntityChangedEvent<HolidayCalendarDay> event) {

        try {

            HolidayCalendarDay holidayCalendarDay = dataManager.load(HolidayCalendarDay.class)
                    .query("select h from mdg_HolidayCalendarDay h " +
                            "where h.id = :id1")
                    .parameter("id1", event.getEntityId())
                    .fetchPlan("holidayCalendarDay-HolidayCal-fetch-plan")
                    .joinTransaction(false)
                    .one();
            HolidayCalendar holidayCalendar = holidayCalendarDay.getHolidayCalendar();
            String name = holidayCalendar.getName();

            //Current year
            myServiceBean.buildHolidayCalendarDetail(holidayCalendarDay.getHolidayCalendar(), "cy");          
        } catch (Exception e) {
            log.error("Error handling Customer changes after commit", e);
        }
    }

It works well except when an entity of HolidayCalendarDay is deleted. When I debugged, I see the cursor is stopped at the query stage.

Hello!

I think in case of HolidayCalendarDay deletion, record is deleted from database so you cannot load it. If you need to get HolidayCalendar reference from deleted record, take a look at example: Entity Events :: Jmix Documentation.

Firstly check the type of operation and then try to get old reference id:

@TransactionalEventListener
void onHolidayCalendarDayChangedAfterCommit(EntityChangedEvent<HolidayCalendarDay> event) {
    HolidayCalendar holidayCalendar;
    if (event.getType() == EntityChangedEvent.Type.DELETED) {
        Id<HolidayCalendar> holidayCalendarId = event.getChanges().getOldReferenceId("holidayCalendar");
        holidayCalendar = dataManager.load(holidayCalendarId)
                .joinTransaction(false)
                .one();
    } else {
        HolidayCalendarDay holidayCalendarDay = dataManager.load(event.getEntityId())
                .joinTransaction(false)
                .one();
        holidayCalendar = holidayCalendarDay.getHolidayCalendar();
        // ...
    }
}

Thanks for your reply but which one are you referring to the link, before commit? it will not work because the service I want to execute requires after commit result. Anyways, it looks like not possible. I am considering use of database level trigger that works!

I meant not using “Before Commit” but the code inside the method. I guess, the problem in the approach you trying to get the reference entity.

Because of deleted entity, you should get a reference using similar code:

Id<HolidayCalendar> holidayCalendarId = event.getChanges().getOldReferenceId("holidayCalendar");
        holidayCalendar = dataManager.load(holidayCalendarId)
                .joinTransaction(false)
                .one();

Thanks for your code-snippet, I shall try to get the holidayCalendarDay first with that code and hope I can derive holidayCalendar from it.