Question

I came across a problem mapping multiple beans with Super CSV. I got a csv file, containing information for multiple beans (per line). But as I can see from examples on the website it is only possible to map each line into one bean (not into two or more beans).

Is there a way of doing this? The only way I can think of is creating a new bean, containing the all beans I need and do a deep mapping, i.e.:

class MultiBeanWrapper {

    Address addreass;
    BankAccount bankAccount;

}

...
String[] FIELD_MAPPING = new String[] 
     {address.street, bankAccount.bankNumber};
...
beanReader.read(MultiBeanWrapper.class, processors));

I didn't try this, because I want to be sure that there is no other / better way.

Thanks for your help Daniel

Was it helpful?

Solution

No, you can't read a line into multiple beans - sorry! (I'm not sure what this would even look like - would you get a List<Object> back?)

You have a few options:

  1. Add a relationship between the objects

    Then can use a mapping like parent.fieldA, parent.child.fieldB. In your scenario Address and BankAccount aren't related semantically, so I'd recommend creating a common parent (see next option)

  2. Add a common parent object

    Then you can use a mapping like parent.child1.fieldA, parent.child2.fieldB. This is what you've suggested, but I'd recommend giving it a better name than Wrapper - it looks like a customer to me!

Oh, and I'd recommend trying stuff out before posting a question - often you'll answer your own question, or be able to give more details which will get you better answers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top