now use more safe onFlush approach to manage entity lifecycle