title: What does EntityManager.flush do and why do I need to use it?
body: <p>I have an EJB where I am saving an object to the database.  In an example I have seen, once this data is saved (EntityManager.persist) there is a call to EntityManager.flush();  Why do I need to do this?  The object I am saving is not attached and not used later in the method.  In fact, once saved the method returns and I would expect the resources to be released.  (The example code does this on a remove call as well.)</p>
  
  <pre><code>if (somecondition) {
      entityManager.persist(unAttachedEntity);
  } else {
      attachedEntityObject.setId(unAttachedEntity.getId());
  }
  entityManager.flush();
  </code></pre>