강의 출처- 인프런 김영한 강사님
https://www.inflearn.com/course/ORM-JPA-Basic
Synchronize datas which happend in the persistence context with the ones in database. In other word, making queries inside the 'Write Behind SQL Store' get carried to the database.
Member member = new Member(110L, "flush");
em.persist(member);
em.flush();
System.out.println("============================");
// not execute query before transaction commit.
tx.commit();
When you wanna see the query being executed before commit, you can do em.flush() liks this.
※ 'Flush' doesn't mean clearing the Persistence Context's 1st Cache. It just makes the queries that are 'stored behind write' carried to the database
flush is called by default when commit transaction is called.
flush is called by default when JPQL query is executed inside the same transaction
em.persist(memberA);
em.persist(memberB);
em.persist(memberC);
List<Member> resultList = em.createQuery
("Select m from Member m", Member.class).getResultList();
In case like code above, there's possibility that resultList can be null because of the 'write behind', so Jpa ensures by default 'flush is executed' if there's JPQL Query inside transaction,