title: Spring JPA - Best way to update multiple fields
body: <p>I'm new to using JPA and trying to transition my code from JdbcTemplate to JPA. Originally I updated a subset of my columns by taking in a map of the columns with their values and created the SQL Update string myself and executed it using a DAO. I was wondering what would be the best way to do something similar using JPA?</p>
  
  <p><strong>EDIT:</strong> <br>
  How would I transform this code from my DAO to something equivalent in JPA?</p>
  
  <pre><code>public void updateFields(String userId, Map&lt;String, String&gt; fields) {
      StringBuilder sb = new StringBuilder();
      for (Entry&lt;String, String&gt; entry : fields.entrySet()) {
          sb.append(entry.getKey());
          sb.append("='");
          sb.append(StringEscapeUtils.escapeEcmaScript(entry.getValue()));
          sb.append("', ");
      }
  
      String str = sb.toString();
      if (str.length() &gt; 2) {
          str = str.substring(0, str.length() - 2); // remove ", "
          String sql = "UPDATE users_table SET " + str + " WHERE user_id=?";
          jdbcTemplate.update(sql, new Object[] { userId },
                  new int[] { Types.VARCHAR });
      }
  }
  </code></pre>