EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(event -> {
Object entity = event.getEntity();
if (entity instanceof BaseEntity baseEntity) {
if (baseEntity.isDisableDelete()) {
throw new IllegalStateException("Unable to remove entity");
}
baseEntity.beforeDelete(entityContext);
}
return false;
});