Menu

#3 Support populating nested properties

2.0.0-beta-1
closed
5
2012-09-16
2008-01-15
wolle
No

It would be very handy if the CSVBeanReader class would be able to populate nested properties of a target bean e.g. if a bean 'Request' contains a nested bean 'User' it would be nice to have a column mapping 'user.name' for the 'Request' bean.
This should be fairly easy to implement using Commons BeanUtils or something similar. The way I solved it was by overriding CsvBeanReader's 'fillObject' method (pretty nasty hack).

Discussion

  • Kasper B. Graversen

    Logged In: YES
    user_id=118125
    Originator: NO

    Hi Wolle78.

    Have you been hacking around with my code?? ;-) I guess it is not something that can be accomplished by implementing a processor. Can you send some code you like to see as part of the supercsv distribution, or supply more concrete code examples on how you'd like to see the configuration of these inner beans? I ask for your input as my concerns are, that such configuration can easily become very complex.

    cheers,
    kasper

     
  • wolle

    wolle - 2008-01-16

    Logged In: YES
    user_id=1981938
    Originator: YES

    Thanks for the quick response, Kasper.

    I'll try to be a bit more specific. Say I've got the following bean 'DummyRequest' I need to populate from a CSV file:

    public class DummyRequest {

    private String productId;
    
    private String productName;
    
    private Date requestDate;
    
    private Long requestNumber;
    
    private DummyUser user;
    

    ...
    }

    with a nested bean 'DummyUser':

    public class DummyUser {

    private String lastName;
    
    private String firstName;
    
    private String name;
    
    private String middleInitials;
    

    ...
    }

    Now, from my understanding what SuperCSV is able to do is to populate the simple non-Javabean properties
    like 'requestNumber, requestDate' using the following line in CSVBeanReader:

    ...
    cache.getSetMethod(resultBean, nameMapping[i]).invoke(resultBean, lineResult.get(i));
    ...

    But I'd like to be able to set properties for class DummyUser too. To map the user name e.g.
    a column mapping such as 'user.name' could be used. In my test class I replaced the above line
    (along with some additional code) to set this nested property using 'Commons BeanUtils':

    ...
    PropertyUtils.setNestedProperty(resultBean, nameMapping[i], lineResult.get(i));
    ...

    This works for the first simple tests I carried out. However you have to make sure that
    the nested bean 'user' is initialized properly to avoid NPEs.

    The reason for all this is I'm trying to add CSV batch support to an application and
    I want to reuse the existing object model.

     
  • Kasper B. Graversen

    Logged In: YES
    user_id=118125
    Originator: NO

    Hi Wolle

    Thanks for the extra code. I have been thinking about it, and it may be something that I'll have a look after the next release. Using beanutils is a nice easy way to get started with a proof of concept, but we have to make sure that the beanUtils are caching the methods lookups etc... could I ask you to generate a huge csv file (just one line and repeat it say 100.000 times). To see the speed impact. If there are hardly none, maybe I should abandon my caching strategy and just use the beanUtils instead all together ;-)

    many thanks
    Kasper

     
  • Roberto Carlos González Flores

    Logged In: YES
    user_id=1804740
    Originator: NO

    Hi, I'm searching for nested property support in your CsvBeanReader's.

    So if You have more information please let me know.

     
  • James Bassett

    James Bassett - 2012-09-09

    Nested property support (a.b.c) and index-based mapping (a.b[1].c) will be included in the upcoming release. There will be a new Reader and Writer that uses Dozer for mapping.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.