1

Have 2 tables Table1 and Table2. Have to read from Table1 and save data to Table2. Before saving to Table2 have to check if the record exist in Table2 if exist then update otherwise insert.

I am new to spring batch and trying to use it. Not sure how this can be done. I see examples online are all flat file to database. Didn't find that does something like above. Any help is appreciated.

opai
  • 253
  • 1
  • 5
  • 14

2 Answers2

1

What you are looking for like below

How can you replicate Hibernate's saveOrUpdate in JPA?

It has noting to do with Spring Batch

Sushil Behera
  • 817
  • 6
  • 20
1

Write a custom ItemWriter (just a pseudo code)

class ItemWriter implement ItemWriter<Table2> {
  public void write(List<Table2> l) {
    for(final Table2 d : l) {
      boolean exists = <Check if item 'd' exists in DB>
      if(exists) then <perform update>
      else <perform insert>
    }
  }
}

If you have a Table2DAO which implements a method performing update/insert you can use an ItemWriterAdapter (google for some example)

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69