title: How to store java.util.ArrayList in Postgresql using Spring
body: <p>I am trying to do a batch-insert of a collection of beans. One of the properties of the bean is an ArrayList. The batch update fails with the exception:</p>
  
  <pre><code> Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.
  </code></pre>
  
  <p>I don't know which Postgresql data type to use for the ArrayList to be compatible. Is there a way I can do the batch update of the beans without changing the data type of its properties?</p>
  
  <hr>
  
  <p>The Bean:</p>
  
  <pre><code>    import java.util.List;
  
      public class SomeBean {
          private int id;
          private List&lt;String&gt; names;
  
          @Override
          public String toString() {
              return "SomeBean [id=" + id + ", names=" + names + "]";
          }
          //Setters and getters
  </code></pre>
  
  <p>Table schema:</p>
  
  <pre><code>CREATE TABLE arraylistexample
  (
    id serial NOT NULL,
    names character varying[]
  )
  </code></pre>
  
  <p>Method to insert the data:</p>
  
  <pre><code>    public void insert(List&lt;SomeBean&gt; beans){
          String sql = "INSERT INTO ARRAYLISTEXAMPLE (NAMES) VALUES (:names)";
  
          SqlParameterSource[] data = SqlParameterSourceUtils.createBatch(beans.toArray());
          pgTemplate.batchUpdate(sql, data);
      }
  </code></pre>
  
  <p>The exception:</p>
  
  <pre><code>Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO ARRAYLISTEXAMPLE (NAMES) VALUES (?, ?)]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.
      at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
      at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:884)
      at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(NamedParameterBatchUpdateUtils.java:40)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:303)
      at some.package.dao.GenericDao.insert(GenericDao.java:45)
      at some.package.runner.FileLogicTester.storingArrayListInDb(FileLogicTester.java:220)
      at some.package.runner.FileLogicTester.main(FileLogicTester.java:86)
  Caused by: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.
      at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1801)
      at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:37)
      at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:46)
      at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:255)
      at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
      at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
      at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
      at org.springframework.jdbc.core.BatchUpdateUtils.setStatementParameters(BatchUpdateUtils.java:63)
      at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.access$000(NamedParameterBatchUpdateUtils.java:32)
      at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils$1.setValues(NamedParameterBatchUpdateUtils.java:47)
      at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:893)
      at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:884)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
      ... 7 more
  </code></pre>
  
  <p>Any suggestions?</p>