1

I have a spring batch program which reads a file and saved into DB. The contents of the file go into different tables. And is saved using Spring Data JPA.

 public class Pojo{
  private AnotherObject sCode;  
  private String aName;
  private String aCode;
 }

 public class AnotherObject{

  private String cName;
  private String cCode;
 }

{"aName","aCode","cName","cCode"}

How do I write 2 different FieldSetMapper and get this functionality or is there any other way

user700
  • 111
  • 1
  • 3
  • 11

1 Answers1

0

Since only one file is read so you need one FieldSetMapper only, but you are writing it to different database then you need composite writer, you can check compositewriter examples referred at https://stackoverflow.com/a/18916786/3530898

Amit K Bist
  • 6,760
  • 1
  • 11
  • 26
  • Ok Amit, I will try.But why do I get error below when springboot starts. cName is in AnotherObject. Why is it referring in Pojo.java Invalid property 'cName' of bean class [Pojo]: Bean property 'cName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? – user700 Oct 19 '17 at 21:07
  • I tried to use CompositeWriter,and it didn't fix my issue. Basically the text file if having values like below, 01|fairfax|33860|Virginia. 01 and fairfax should go to table a and 33860|Virginia should go to tableb. How can I construct writer and reader for this case. – user700 Oct 20 '17 at 15:38
  • whats the logic to differentiate between entering data in table1 comapre to tableb, apply that logic in the writers, if logic 1 is true then tablea and logic2 is true then tableb – Amit K Bist Oct 20 '17 at 16:51
  • https://gist.github.com/gnosis23/11258003 has nice logic, to filter the data and then delegate it to correct writer – Amit K Bist Oct 20 '17 at 16:55
  • I know by seeing the data, but in code while reading the line or writing how can I find it where to break and go to next writer. Can you provide a sample – user700 Oct 20 '17 at 17:00
  • How would someone or code will know the logic, it has to be you who has to decide the logic how to break the data. If you have 10 lines in the file then you need to have a logic which data from which lines will go to tablea and which ones will go to tableb. You can apply logic in compositewriter, but logic has to be created by you. – Amit K Bist Oct 20 '17 at 17:04
  • the github link isn't working. So you say I have to have a custom Compositewriter – user700 Oct 20 '17 at 17:13
  • Yes, you need to create custom CompositeWriter – Amit K Bist Oct 20 '17 at 20:49