title: Spring batch insert/update to postgres using prepared statement
body: <p>I am trying to insert records to postgres table using prepared statements but I am getting following errors while using insert statements:</p>
  
  <pre><code>[main] ERROR org.springframework.batch.core.step.AbstractStep - Encountered an error executing step someStep in job someJob
  org.springframework.dao.EmptyResultDataAccessException: Item 0 of 1 did not update any rows: [[com.abc.abc.abc.dao.orm.SomeORM@6653en,com.abc.abc.abc.dao.orm.SomeORM@75f04e]
     at org.springframework.batch.item.database.JdbcBatchItemWriter.write(JdbcBatchItemWriter.java:202)
  </code></pre>
  
  <p>My writer is :</p>
  
  <pre><code>package com.somepackage.writer;
  
  public class trialClass implements ItemPreparedStatementSetter&lt;List&lt;someORM&gt;&gt; {
  
      @Override
      public void setValues(List&lt;someORM&gt; itemList,java.sql.PreparedStatement ps) throws SQLException,EmptyResultDataAccessException {
           try {
      for (ORM item : itemList) {
      for (int i = 0;i&lt;itemList.size();i++)
          {
      ps.setInt(1, itemList.get(i).getPropOne());                         
      ps.setString(2, itemList.get(i).getPropTwo());
       ps.setInt(3, itemList.get(i).getPropThree());
       ps.setInt(4, itemList.get(i).getPropFour());
      ps.addBatch();
  }
  int[] rs = ps.executeBatch();
         } catch (Exception e) {
       e.printStackTrace();
      }}}
  </code></pre>
  
  <p>My xml file is something like this:</p>
  
  <pre><code>&lt;beans:bean id="beanId" scope="step"
                class="org.springframework.batch.item.support.CompositeItemWriter"&gt;
                &lt;beans:property name = "delegates"&gt;
                       &lt;util:list&gt;
                       &lt;beans:ref bean= "someId"/&gt;
                       &lt;beans:ref bean= "someOtherId"/&gt;
                       &lt;/util:list&gt;
                &lt;/beans:property&gt;
                &lt;/beans:bean&gt;
  
  &lt;beans:bean id = "someId" scope = "step" 
                class="org.springframework.batch.item.database.JdbcBatchItemWriter"&gt;
                &lt;beans:property name="dataSource" ref="someSource" /&gt;
                &lt;beans:property name="sql" value = "${fetch.somequery.sql}" &gt;  
                &lt;/beans:property&gt;
                &lt;beans:property name="itemPreparedStatementSetter" ref="someRef" /&gt;
         &lt;/beans:bean&gt;
  </code></pre>
  
  <p>I am getting error only while inserting the records, updates are working fine
  (postgres table has primary key on first column)
  (In both the cases the records are getting inserted/updated in the postgres table but still getting this error in case of insert)</p>
  
  <p>and I am using query as :</p>
  
  <pre><code>fetch.somequery.sql =INSERT INTO table1 (col1, col2, col3) SELECT ?,?,? WHERE NOT EXISTS (SELECT 1 FROM table1 WHERE col1= ?);
  </code></pre>
  
  <p>but when I am using plain insert query I am getting error as :</p>
  
  <pre><code>Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key violates unique constraint "table1_pkey"  (seg576 sdw24:40000 pid=398877)
     at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
  </code></pre>
  
  <p>I have made sure that the records are not duplicate but still getting this error</p>
  
  <p>Where am I doing it wrong?
  Can I use ORM to update data to postgres. If yes the how?
  Can I use jdbc template to update data to postgres. If yes the how?</p>
  
  <p>How to handle this exception?</p>