1

I need to sort pojo of different data type like Student,employee,patient using age and store it into array. Then write it to flat file using beanio.

By json i am sending request which can have array of student,employee and patient .I have 3 pojo at java side like student,employee,patient to store data from json request.

i am able to merge and then sort all array of objects like student,employee,patient into single array of class which is base class of student,employee,patient like Human. Human class i have to make so i can sort all 3 child class using Comparator by property age.

class SortbyAge implements Comparator<Human>
{ 
    // Used for sorting in ascending order of 
    // age
    public int compare(Human a, Human b) 
    { 
        return a.getAge() - b.getAge(); 
    } 
} 

By here everything is fine . I am able to sort data depending on age and store it into Human Array.

problem is when i am writing sorted data to flat file using beanio .

**when i am writing data to Flat file i am getting exception below exception

org.beanio.BeanWriterException: Bean identification failed: no record or group mapping for bean class 'class [Lcom.amex.ibm.model.Human;' at the current position**

i have written all 4 tags into xml file like below.

<record name="student" class="com.amex.ibm.model.Student"  occurs="0+" maxLength="unbounded">
          <field name="name" length="3"/>
          <field name="age" length="8"/>      
          <field name="address" length="15"/>
</record>
<record name="employee" class="com.amex.ibm.model.Employee"  occurs="0+" maxLength="unbounded">
          <field name="name" length="3"/>
          <field name="age" length="8"/>
          <field name="address" length="15"/>
</record>
<record name="patient" class="com.amex.ibm.model.Patient"  occurs="0+" maxLength="unbounded">
          <field name="name" length="3"/>
          <field name="age" length="8"/>
          <field name="address" length="15"/>
</record>
<record name="human" class="com.amex.ibm.model.Human"  occurs="0+" maxLength="unbounded">
          <field name="age" length="3"/>
    </record>

How to define Parent class mapping in bean IO??

Monu
  • 21
  • 3
  • How do you pass the objects to the `BeanWriter`? All 3 classes - Student, Employee & Patient have the same fields in the mapping file. Are those the only fields you write to the flat file? Or have you left out fields to make the question smaller? If you only have those 3 fields, why don't you put all of them into the `Human` class and then you only need to have one mapping for the `Human` class? – nicoschl Nov 02 '18 at 10:16
  • ConfigFile parameter in above method is filename of below xml. ..... – Monu Nov 03 '18 at 07:02
  • Student,Employee,Patient have many other properties which are unique to same class like treatmentStartDate , diseaseName for Patient Class and StreamName , AdmisssionDate for Student etc. – Monu Nov 03 '18 at 07:06
  • //code to write java bean to Flat File public void generateFlatFile(String configFile,Submission s){ StreamFactory f=StreamFactory.newInstance(); ClassLoader classloader=Thread.currentThread().getContextClassLoader(); InputStream is=classloader.getResourceAsStream(configFile); f.load(is); BeanWriter b=f.createWriter("fStream",new File("C:\\list.txt")); Student [] Student=s.getListStudent(); Employee [] Employee=... Patient [] Patient= Human [] FinalArray=//Student+Employee+patient; Arrays.sort(listFinalArray, new SortbyAge()); //sorting code as per Age b.write(listFinalArray); } – Monu Nov 03 '18 at 07:12

1 Answers1

0

The problem you are seeing is that BeanIO doesn't know how to map an array of type Human You need to pass each of the individual objects to BeanIO to write it out to your file. Try this, by looping over your array and then pass each of the objects to BeanIO.

Change

b.write(listFinalArray);

to

for (int i = 0; i < listFinalArray.length; i++) {
  b.write(listFinalArray[i]);
}

or less typing:

for (final Human human : listFinalArray) {
  b.write(human);
}
nicoschl
  • 2,346
  • 1
  • 17
  • 17