Why do we have to manually flush() the EntityManager in a extended PersistenceContext?

后端 未结 3 1113
广开言路
广开言路 2021-02-01 10:27

In our J2EE application, we use a EJB-3 stateful bean to allow the front code to create, modify and save persistent entities (managed through JPA-2).

It looks something

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 10:54

    If you use extended persistence context all operations on managed entities done inside non-transactional methods are queued to be written to the database. Once you call flush() on entity manager within a transaction context all queued changes are written to the database. So in other words, the fact that you have a transactional method doesn't commit the changes itself when method exits (as in CMT), but flushing entity manager actually does. You can find full explanation of this process here

提交回复
热议问题