title: Execute query with native query (update query) in Hibernate 5.x
body: <p>I am currently upgrading our application and we are migrating from <strong>Hibernate 3.x</strong> and <strong>Spring/Spring Security 3.x</strong> to <strong>Hibernate 5.x</strong> and <strong>Spring/Spring Security 5.x</strong> respectively. We had some methods that executed native sql update queries (example 1), but after upgrading to <strong>5.x</strong> the methods started throwing <strong>TransactionRequiredException: Executing an update/delete query</strong> exceptions. Well I tried adding the <strong>@Transactional</strong> annotation on the methods but it doesn't seem to help. I will share the old method and the upgraded method (example 2).</p>
  
  <p>I don't understand how it is not working on the new version, did Hibernate change the way they treat native sql queries? Thanks for the responses.</p>
  
  <p><strong>Example 1</strong></p>
  
  <pre><code>public void myTestMethod() {
      String sql = "Update myTable set State = 1";
      Session session = getSessionFactory().openSession();
      Query query = session.createSQLQuery(sql);
      query.executeUpdate();
      session.close();
  }
  </code></pre>
  
  <p><strong>Example 2</strong></p>
  
  <pre><code>public void myTestMethod() {
      String sql = "Update myTable set State = 1";
      Session session = sessionFactory.openSession();
      Query query = session.createNativeQuery(sql);
      query.executeUpdate();
      session.close();
  }
  </code></pre>
  
  <p>What am I doing wrong here? How can I execute this update without changing much in the methods (we have thousands of methods implemented). The <code>sessionFactory</code> in the example 2 is injected with <strong>@Inject</strong> annotation.</p>