0

I have created a spring batch application , which reads data from .csv file imports data in to POSTGRES database successfully .

When i try to import another .csv file , the batch statements fails with DuplicateKeyException . Person_id is my PRIMARY KEY

String sql = "INSERT INTO Person " + "( fullname, date_of_birth,person_id) VALUES (?, ?)";
    getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException{

            Person Person = Persons.get(i);
        //  ps.setLong(1, Person.getId());
            /*ps.setString(2, Person.getFirstName());
            ps.setString(3, Person.getLastName());
            */
            //ps.setString(1, Person.getLatest_data());


            try{

            ps.setString(1, Person.getFullName());
            ps.setString(2, Person.getDate_oF_birth);
            ps.setString(3, Person.getPersin_id);
        }
            public int getBatchSize() {
                return Persons.size();
            }
    });

What is the best option to deal with this UPSERT ,Is there any other ways of dealing this issue as i think this is a very common usecase, batch should update the row if the data already exist in database .

Thanks for your advise. Is using JPAItemWriter is the only way to handle this issue can some one point me to sample code for using JPA Itemwriter /repositorywriter for spring Batch

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Angel
  • 57
  • 1
  • 10
  • what postgres version you're using? Also you have written simple insert query which will never handle if duplicate key found. – Sam Aug 24 '17 at 12:29
  • Hi @Minella -The question is in regards to SPring batch – Angel Aug 24 '17 at 13:46
  • This has nothing to do with spring-data or jpa or boot. I removed the tags. and it really is a duplicate since it is a pure sql/postgres question. – Jens Schauder Aug 24 '17 at 13:50

0 Answers0