title: javax.persistence.TransactionRequiredException Error: jpa 2 + hibernate - Native Query
body: <p>I'm getting the above error when I'm trying to update sphinx real time indexes. Since sphinx uses a database very close to mysql, I'm trying to use entity manager to update indexes. But sphinxql isn't a full mysql database so if I use entitymanager.merge method the sql generated isn't understood by sphinxql. Therefore, I decided to use createNativeQuery to insert new indices or delete indices from the Real Time Index. However, I'm getting the exception in the title when I invoke .executeUpdate method of createNativeQuery.</p>
  
  <p>This is my Dao:</p>
  
  <pre><code>public interface RTIndexGeoDao {
  
      public boolean deleteIndex(long id);
  
      public boolean insertIndex(long id, long agentActivityId, long agentId, double latitude, double longitude);
  }
  </code></pre>
  
  <p>This is my Implementation of Dao:</p>
  
  <pre><code>import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.stereotype.Repository;
  
  @Repository(value = "rtIndexGeoDao")
  public class JPARTIndexDao implements RTIndexGeoDao {
  
      private EntityManager em;
  
      @PersistenceContext(unitName = "rtIndexPU")
      public void setRtIndexEntityManager(EntityManager em)
      {
          this.em = em;
      }
  
      public boolean deleteIndex(long id) {
          //This is to update our geodistance index
          try
          {
              this.em.createNativeQuery("delete from rt_geo where agent_agent_id = ?1").setParameter(1, id).executeUpdate();
  
  
              return true;
          }
          catch (Exception ex)
          {
              return false;
          }
      }
  
      public boolean insertIndex(long id, long agentActivityId, long agentId,
              double latitude, double longitude) {
          try
          {
              this.em.createNativeQuery("insert into rt_geo(id, agent_activity_id, agent_agent_id, latitude, longitude) values(?1, ?2, ?3, ?4, ?5)")
              .setParameter(1, id)
              .setParameter(2, agentActivityId)
              .setParameter(3, agentId)
              .setParameter(4, latitude)
              .setParameter(5, longitude).executeUpdate();
  
              return true;
          }
          catch (Exception ex)
          {
              return false;
          }
      }
  
  }
  </code></pre>
  
  <p>This is my RTIndexManager:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  import org.springtest.mavenspringapp.repository.RTIndexGeoDao;
  
  @Component
  @Transactional
  public class SphinxRTIndexManagerImpl implements SphinxRTIndexManager {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      @Autowired
      private RTIndexGeoDao rtIndexGeoDao;
  
      public boolean deleteIndex(long id) {
          return rtIndexGeoDao.deleteIndex(id);
      }
  
      public boolean insertIndex(long id, long agentActivityId, long agentId,
              double latitude, double longitude) {
          return rtIndexGeoDao.insertIndex(id, agentActivityId, agentId, latitude, longitude);
      }
  
  }
  </code></pre>
  
  <p>This is my persistence.xml</p>
  
  <pre><code>&lt;persistence-unit name="rtIndexPU" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>This is the controller:</p>
  
  <pre><code>@Autowired
      private SphinxRTIndexManager rtIndexManager;
  private AgentActivity createActivity(Agent agent, double latitude, double longitude, String agentActivityDescription)
      {
  //Steps to update indexes
          boolean deleteEntry = rtIndexManager.deleteIndex(agent.getAgentId());
          if (!deleteEntry) return null;
  </code></pre>
  
  <p>UPDATE: I'm posting my applicationContext.xml</p>
  
  <pre><code>&lt;!-- enabling annotation driven configuration /--&gt;
      &lt;context:annotation-config/&gt;
  &lt;bean id="rtIndexEntityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="rtIndexDataSource"
          p:jpaVendorAdapter-ref="jpaAdapter"&gt;
          &lt;property name="loadTimeWeaver"&gt;
              &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/&gt;
          &lt;/property&gt;                             
          &lt;property name="persistenceUnitName" value="rtIndexPU"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;bean id="rtIndexTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="rtIndexEntityManagerFactory"/&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/&gt;
  </code></pre>
  
  <p>I have another persistence unit and every manager, dao, etc works. But when I use createNativeQuery.executeUpdate on entitymanager whose persistenceContext is rtIndexPU, this exception occurs: </p>
  
  <p>javax.persistence.TransactionRequiredException: Executing an update/delete query</p>
  
  <p>I've searched at every question available on the internet, but cannot have this issue solved. Thanks in advance.</p>