title: Is it necessary to call a flush() (JPA interface) in this situation?
body: <p>Because calling a flush() to get every entities persist from memory to database. So if I use call too much unnecessary flush(), it could take much time therefore not a good choice for the performance. Here is a scenario that I don't know when to call a flush()?</p>
  
  <pre><code>//Order and Item have Bidirectional Relationships
  Order ord = New ord("my first order");
  Item item = New Item("tv",10);
  
  //...process item and ord object
  
  em.persist(ord);//em is an instance of EntityManager
  em.flush();// No.1 flush()
  
  item.setOrder(ord);
  em.persist(item);
  
  Set&lt;Item&gt; items= new HashSet&lt;Item&gt;();
  items.add(item);
  ord.setItems(items);
  
  em.flush();// No.2 flush()
  </code></pre>
  
  <p><strong>My question is: calling of the No.1 flush could be avoid or not?</strong></p>
  
  <p>The things I worried is: in order to do the <em>item.setOrder(ord)</em>, we need an database id of ord. And calling only <em>em.persist(ord)</em> cannot generate an database id, so I have to call the <em>em.flush()</em> before <em>item.setOrder(ord)</em>. So what's your opinion guys?</p>
  
  <p>Thanks in advance.</p>