In your first example, the changes to the data are reflected in database after encountering flush, but it is still in transaction.
But in second example, you are committing transaction immediately. Therefore the changes are made into the database & transaction also ends there.
Sometimes, flush may be useful to persist the data in between the ongoing transaction & then finally commit the changes afterwards. So you can also rollback the previous changes if there occurs some problem afterwards, like for batch insert/update.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…