Announcement Announcement Module
Collapse
No announcement yet.
Mapping a bean inside a bean with BeanRowPropertyMapper Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mapping a bean inside a bean with BeanRowPropertyMapper

    I searched for this topic in the forums, but couldn't find it. I also apologize for probably not using the correct terminology. Here it goes.

    I have a couple of beans as:

    Code:
    BeanA {
    String a;
    BeanB beanB;
    }
    
    BeanB {
    String b;
    }
    and then in my sql I have something to like

    Code:
    SELECT 
    a as a,
    b as beanB.b
    FROM XYZ;
    And then I am using the query method for jdbcTemplate as follows:

    Code:
    List<BeanA> beanAList = 
    getJdbcTemplate().query(
    <SELECT SQL GOES HERE>, 
    new BeanPropertyRowMapper<BeanA (BeanA.class)
    );
    However, the String b, which is inside BeanB, which is inside BeanA does not get populated.

    Is there a special implementation of BeanPropertyRowMapper that permits column names to be mapped to beans inside beans using the dot notation? I could definitely look into writing a custom bean property row mapper, but I find it rather incredulous that the Spring team did not implement this by default. I feel like I am missing something.

    Can someone please guide?

    Thanks.

    Abhishek Goel

  • #2
    Yeah, I've been trying the same thing, with no luck.
    I even tried this in my SQL...

    SELECT col1 as [beanB.b]
    FROM table

    but could not get it to work.
    There must be a solution to this; I don't believe RowMapper is a solution, what would be the point of manually writing a custom row mapper to iterate through the result set? my feeling is that was the whole point behind the bean row property mapper.

    some help from the community would be greatly appreciated.

    Comment


    • #3
      The whole point of a row mapper is that you DON'T iterate over the result set (the iteration is done for you by spring) that has nothing to do with the fact which implementation you use (the name of the method mapRow should be a give away). Also if you read the docs coming with the BeanPropertyRowMapper you will notice that its main intended use is to map simple types and thus not complex types.

      Make sure that all the getters and setters are there also make sure that there is already an empty instantiated instance of BeanB assigned, spring will not create an nested instance for you.

      Comment

      Working...
      X