Question about flushing with JPA before a query is called

后端 未结 1 1001
栀梦
栀梦 2021-01-06 11:43

Just a quick question, but is the flush necessary in this code? Note this would be inside of a JPA transaction.

User user = new User();
em.persist(user);

em         


        
相关标签:
1条回答
  • 2021-01-06 12:22

    In the first case flush is needed as long as User has an autogenerated id, since it's not assigned before flush. If id is not generated, em.find() will return the same instance from the persistence context, so flush is not needed.

    In the second case explicit flush is not needed because JPA performs flush before executing a query automatically (if flush mode is AUTO that is by default, otherwise explicit flush is needed).

    0 讨论(0)
提交回复
热议问题