title: JDBC Spring 4 NamedParameterJdbcTemplate with enum
body: <p>I have been using <code>JdbcTemplate</code> very happyly but then I got convinced to use <code>NamedParameterJdbcTemplate</code>. However not enjoying this so far sometimes or all the times.</p>
  
  <p>Lets say I have a Person class as below :</p>
  
  <pre><code>public class Person implements Serializable {
  
      private int age;
      private String name;
      private long socialId;
      private Gender gender;
      private String email;
  
      enum Gender {
          MALE, FEMALE
      }
  
  }
  </code></pre>
  
  <p>And I am trying to insert the list of person as below:</p>
  
  <pre><code>SqlParameterSource[] params 
  = SqlParameterSourceUtils.createBatch(personList.toArray());
  
              getNamedJdbcTemplate().batchUpdate(insertSql, params);
  </code></pre>
  
  <p>However it fails with message below</p>
  
  <pre><code>PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO
  
  at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:230)
      at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:169)
      at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:169)
      at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:402)
      at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:235)
      at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:166)
      at org.springframework.jdbc.core.BatchUpdateUtils.setStatementParameters(BatchUpdateUtils.java:65)
      at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.access$000(NamedParameterBatchUpdateUtils.java:32)
      at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils$1.setValues(NamedParameterBatchUpdateUtils.java:48)
      at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:999)
      at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:989)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
      ... 41 more
  </code></pre>
  
  <p>I debugged and can see that in <code>StatementCreatorUtils</code> class it will always fail if it is of Enum type in method</p>
  
  <pre><code>private static void setValue(PreparedStatement ps, int paramIndex
  </code></pre>
  
  <p>section of code....</p>
  
  <pre><code>else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
              if (isStringValue(inValue.getClass())) {
                  ps.setString(paramIndex, inValue.toString());
              }
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <p>Can someone please let me know any workaround for this? Or I need to do old way of setting data using placeholders( ?).</p>
  
  <p>Many thanks</p>