title: Spring with Hibernate JPA - EntityManager is closed exception
body: <p>I am using <code>EntityManagerFactory(injected)</code> to create entityManager each time I need to access the db. I have the following code which throws,</p>
  
  <p><code>org.springframework.dao.InvalidDataAccessApiUsageException: EntityManager is closed</code></p>
  
  <pre><code>entityManager = entityManagerFactory.createEntityManager();
  
  List&lt;Object&gt; list = entityManager.createQuery("FROM Class").getResultList();
  
  entityManager.close();
  
  return list.toArray(new Object[list.size()]);
  </code></pre>
  
  <p>I am creating an EntityManager, querying db for some results, closing <code>EntityManager</code> and returning result to someone who wants those results.
   I think I can figure out what's wrong. The <code>getResultList()</code> returning objects which are managed. So, trying to access them after closing the <code>EntityManager</code> causing the error(Well, I think...). I tried <code>entityManager.clear()</code> before closing it. I got the same error. I tried detaching 
   each object from the list(<code>entityManager.detach(obj)</code>), before closing it. Still, I got the same error. I tried annotating with <code>@Transactional(readOnly=true)</code> on the method. No use. </p>
  
  <p>I am not going to modify the objects. I just need them to display on the UI. Can you give me a solution for that? I am using Spring boot + hibernate JPA + AngularJS + Postgresql.</p>
  
  <p>(PS: I can't left the entityManager open. Left it open, I got connection limit exceeded error. It is not a best way either)</p>