title: How can I detach() a collection of JPA managed entities?
body: <p>Suppose I run a JPQL query and get its results:</p>
  
  <pre><code>final Iterable&lt;? extends Greeble&gt; greebles = someTypedJPQLQueryReturningZillionsOfGreebleEntities.getResultList();
  assert greebles != null;
  </code></pre>
  
  <p>Suppose further I'm in a JTA transaction.</p>
  
  <p>At this point, according to the JPA specification, all <code>Greeble</code> entities in the returned <code>List</code> will be managed (tracked in the persistence context).</p>
  
  <p>As it happens, I don't want them to be tracked in the persistence context.  I've found that there is a massive amount of overhead if they are, and various of my team members will inadvertently call setters on various <code>Greeble</code>s and will thereby cause <code>UPDATE</code>s to go to the database at <code>EntityManager#flush()</code> time when really none should.</p>
  
  <p>I do not want to <code>clear()</code> the persistence context, as there are various entities in there that <em>should</em> be tracked/managed.</p>
  
  <p>I have a vague uncertain nasty feeling about doing the following, but it seems like it's the only way:</p>
  
  <pre><code>for (final Greeble greeble : greebles) {
    if (greeble != null) {
      entityManager.detach(greeble);
    }
  }
  </code></pre>
  
  <p>Now, the reason I have a vague uncertain nasty feeling is that it seems to me the JPA provider might have all sorts of tricks up its sleeve to avoid actually loading the full query results list, and that traversing this list in its entirety might defeat such tricks.  Maybe this is an unfounded concern.  At any rate, I'm loathe to sit here in a tight loop detaching the entities one by one.  Perhaps I should not be.</p>
  
  <p><b>Is there some other way I've overlooked to efficiently detach zillions of entities from an <code>EntityManager</code>/persistence context?</b></p>