JPA flush vs commit

em.flush() - It saves the entity immediately to the database with in a transaction to be used further and it can be rolled back.

em.getTransaction().commit - It marks the end of transaction and saves all the chnages with in the transaction into the database and it can't be rolled back.

Refer https://prismoskills.appspot.com/lessons/Hibernate/Chapter_14_-_Flush_vs_Commit.jsp


if we call EntityTransaction.commit(), does it automatically call EntityManager.flush()?

Yes

what is the difference?

In flush() the changes to the data are reflected in database after encountering flush, but it is still in transaction.flush() MUST be enclosed in a transaction context and you don't have to do it explicitly unless needed (in rare cases), when EntityTransaction.commit() does that for you.

Source