title: Spring & Hibernate - Make native queries run in the same transaction as @Transactional
body: <p>Is it possible to create native queries that make use of an existing transaction created via @Transactional?
  Most of the questions here seem to be about making native queries at all. Also, answers such as the one from <a href="https://stackoverflow.com/questions/29196735/spring-hibernate-transaction-native-sql-rollback-failure">Spring + Hibernate Transaction -- Native SQL rollback failure</a> suggest it might not be possible.</p>
  
  <p>What I do to test the isolation is to run some deletes and add a breakpoint to investigate the database.</p>
  
  <p>Here is what I tried so far:</p>
  
  <pre><code>@Transactional(value = "someManager")
  public class SpringJpaSomeDao implements SomeDao {
  
  @PersistenceContext(unitName = "someUnit")
  @Qualifier("entityManagerFactorySome")
  private EntityManager em;
  
  
  @Resource
  @Qualifier("someManager")
  private PlatformTransactionManager transactionManager;
  
  @Override
  @Transactional(value = "someManager", propagation = Propagation.SUPPORTS)
  public void runNative(String sql){
      TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
      transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
      transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  
          @Override
  
          protected void doInTransactionWithoutResult(TransactionStatus status) {
  
              em.createNativeQuery(sql).executeUpdate();
  
          }
  
      });
  
  
  
  
  
  }
  </code></pre>
  
  <p>Some part of the persistence.xml</p>
  
  <pre><code>&lt;persistence-unit name="someUnit" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;non-jta-data-source&gt;java:comp/env/jdbc/someDS&lt;/non-jta-data-source&gt;
      &lt;!-- some classes here --&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
          &lt;!-- some dialect --&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>The code is invoked from some controller which also has @Transactional annotations giving the deletes to the Dao.</p>
  
  <pre><code>@Transactional(value = "someManager", propagation = Propagation.SUPPORTS)
  public void deleteEntireDatabase() {
  
      List&lt;String&gt; deletions = new ArrayList&lt;&gt;();
      deletions.add("DELETE FROM something;");
  
  
      for (String currentDeletion : deletions) {
          someDao.runNative(currentDeletion);
      }
  
  
  }
  
  @Override
  @Transactional(value = "someManager", propagation = Propagation.REQUIRES_NEW, rollbackFor = {Exception.class})
  public void deleteAndFill(JobExecutionProgress progress) {
      deleteEntireDatabase();
      // more code
  }
  </code></pre>
  
  <p>Excerpt from spring-dao.xml:</p>
  
  <pre><code>&lt;tx:annotation-driven  /&gt;
  
  &lt;bean id="entityManagerFactorySome" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="someDataSource" /&gt;
      &lt;property name="persistenceUnitName" value="someUnit" /&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;!-- some dialect --&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="someDao" class="xyz.model.controller.SpringJpaSomeDao"/&gt;
  
  &lt;bean id="someManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactorySome" /&gt;
      &lt;qualifier value="someManager"&gt;&lt;/qualifier&gt;
  &lt;/bean&gt;
  
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  </code></pre>
  
  <p>Of course, I also tried some variations such as different Propagations, and using the entityManager obtained from elsewhere:</p>
  
  <pre><code>    EntityManagerFactory emf = ((JpaTransactionManager) transactionManager).getEntityManagerFactory();
  
      EntityManager em2 = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
  </code></pre>
  
  <p>Now, what I will do if everything else fails is manual transaction management.</p>
  
  <p>Is this something that has worked for one of your applications or is it unknown if this might be a setup problem?</p>