It is usually not recommended to explicitly call flush if this is not necessary. Hibernate usually automatically calls Flush at the end of the transaction, and we must let it work. Now there are some cases where you may need to explicitly call flush, where the second task depends on the result of the first Persistence task, both of which are inside the same transaction.
For example, you may need to save a new Entity, and then use the Id of this object to perform another task inside the same transaction, in which case it needs to explicitly clear the object.
@Transactional void someServiceMethod(Entity entity){ em.persist(entity); em.flush()
Also note that explicit cleanup does not cause the database commit, the database commit only occurs at the end of the transaction, so if any Runtime error occurs after calling flush, the changes will be undone anyway.
adn.911 Nov 21 '17 at 13:24 2017-11-21 13:24
source share