title: How to execute custom SQL query with spring-managed transactional EntityManager
body: <p>I have an application built on Spring. I let the Spring do the all <code>@Transactional</code> magic and everything works fine as long as I operate on my entities that are mapped to Java objects.</p>
  
  <p>However, when I want to do some custom job on a table that is not mapped to any of my Java entities, I'm stuck. Some time ago, I found a solution to execute a custom query like this:</p>
  
  <pre><code>// em is instance of EntityManager
  em.getTransaction().begin();
  Statement st = em.unwrap(Connection.class).createStatement();
  ResultSet rs = st.executeQuery("SELECT custom FROM my_data");
  em.getTransaction().commit();
  </code></pre>
  
  <p>When I try this with the entity manager injected from Spring with the <code>@PersistenceContext</code> annotation, I receive almost obvious exception:</p>
  
  <pre><code>java.lang.IllegalStateException: 
  Not allowed to create transaction on shared EntityManager - 
  use Spring transactions or EJB CMT instead
  </code></pre>
  
  <p>I finally managed to extract non-shared Entity Manager like this:</p>
  
  <pre><code>@Inject
  public void myCustomSqlExecutor(EntityManagerFactory emf){
      EntityManager em = emf.createEntityManager();
      // the em.unwrap(...) stuff from above works fine here
  }
  </code></pre>
  
  <p>Nevertheless, I find this solution neither comfortable nor elegant. I just wonder if there is any other way to run custom SQL queries in this Spring-transactional-driven environment?</p>
  
  <p>For those who are curious - this problem appeared when I tried to create user accounts in my application and in the related forum at once - I did not want the forum's users table to be mapped to any of my Java entities.</p>