3

I am trying to map a bean to a CSV file with selected columns (not all columns) but the problem that my bean has other nested beans as attributes.

Class Student {

@CsvBindByName
private String s_id;

@CsvBindByName
private String name;

@CsvRecurse
private List<Book> books;
}

Class Book {

@CsvBindByName
private String bookName;
private String bookAuthor;
}

I want to map this object to csv file with below format

s_id;name;bookName

I have tried with below code but not getting bookName in csv

ColumnPositionMappingStrategy<Student> mapStrategy
                    = new ColumnPositionMappingStrategy<>();

            mapStrategy.setType(Student.class);

            String[] columns = new String[]{"s_id", "name", "bookName"};
            mapStrategy.setColumnMapping(columns);

            StatefulBeanToCsv<Student> btcsv = new StatefulBeanToCsvBuilder<Student>(writer)
                    .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                    .withMappingStrategy(mapStrategy)
                    .withSeparator(';')
                    .build();

            btcsv.write(students);

Could anyone please help me.

0 Answers0