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