Question

I am creating MVC web application in Spring Framework and I need to transform rows from Apache DBUtils resultset into JavaBeans that is composed from nested objects.

With respect to a very few examples I found I created this RowProcessor implementation.

public class MonthOrderCountHandler extends BasicRowProcessor {

    @Override
    public Object toBean(ResultSet rs, Class type) throws SQLException {

        // Year
        Year year = new Year();
        year.setYearNo(rs.getInt("yearNo"));
        year.setYear4(rs.getString("year4"));
        year.setYear2(rs.getString("year2"));

        // Quarter
        Quarter quarter = new Quarter();
        quarter.setQuarter(rs.getInt("quarter"));

        // Month
        Month m = new Month();
        m.setYear(year);
        m.setQuarter(quarter);
        m.setMonthAbbreviation(rs.getString("monthAbbreviation"));
        m.setMonthName(rs.getString("monthName"));
        m.setMonthNo(rs.getInt("monthNo"));

        // Final bean
        MonthOrderCount result = new MonthOrderCount();
        result.setMonth(m);
        result.setOrderCount(rs.getInt("orderCount"));

        return result;

    }
}

Question: I would like to know know how to use this row processor in my DAO object and if this implementation is correct?


Commonly I transform rows into JavaBeans in this way:

ResultSetHandler<List<MonthOrderCount>> listUrlHandler = new BeanListHandler<>(MonthOrderCount.class);

But in my situation first Ineed to create nested objects and then create a final JavaBean, so I assume I need custom row processor.


Structure of my domain objects is:

MonthOrderCount class:

public class MonthOrderCount {    
    private Month month;
    private int orderCount;
}

Month class:

public class Month {
    private Quarter quarter;
    private Year year;
    private int monthNo;
    private String monthName;
    private String monthAbbreviation;
}

Quarter class:

public class Quarter {
    private int quarter;
    private String abbreviation;
}

Year class:

public class Year {
    private int yearNo;
    private String year2;
    private String year4;
}

EDIT: I am asking because my result looks like this. orderCount variable is properly filled but month is null in all instances. Buw what is the most weird for me - toBean() method is never called.

2013-03-10 17:09:46 INFO ChartDataService:29 - [MonthOrderCount{month=null, orderCount=1863}, MonthOrderCount{month=null, orderCount=2262}, MonthOrderCount{month=null, orderCount=2531}, MonthOrderCount{month=null, orderCount=2379}, MonthOrderCount{month=null, orderCount=2106}, MonthOrderCount{month=null, orderCount=1498}, MonthOrderCount{month=null, orderCount=1300}, MonthOrderCount{month=null, orderCount=1578}, MonthOrderCount{month=null, orderCount=2385}, MonthOrderCount{month=null, orderCount=2991}, MonthOrderCount{month=null, orderCount=2219}, MonthOrderCount{month=null, orderCount=1943}, MonthOrderCount{month=null, orderCount=264}]

Was it helpful?

Solution

If you are transforming resultset into List of JavaBeans you need to override toBeanList() and not toBean() method.

Final handler class overrided BasicRowProcessor looks like this:

public class MonthOrderCountHandler extends BasicRowProcessor {

    @Override
    public List toBeanList(ResultSet rs, Class clazz) {
        try {
            List newlist = new LinkedList();
            while (rs.next()) {
                newlist.add(toBean(rs, clazz));
            }
            return newlist;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public Object toBean(ResultSet rs, Class type) throws SQLException {

        // Year
        Year year = new Year();
        year.setYearNo(rs.getInt("yearNo"));
        year.setYear4(rs.getString("year4"));
        year.setYear2(rs.getString("year2"));

        // Quarter
        Quarter quarter = new Quarter();
        quarter.setQuarterNo(rs.getInt("quarterNo"));

        // Month
        Month m = new Month();
        m.setYear(year);
        m.setQuarter(quarter);
        m.setMonthAbbreviation(rs.getString("monthAbbreviation"));
        m.setMonthName(rs.getString("monthName"));
        m.setMonthNo(rs.getInt("monthNo"));

        // Final bean
        MonthOrderCount result = new MonthOrderCount();
        result.setMonth(m);
        result.setOrderCount(rs.getInt("orderCount"));

        return result;

    }
}

I hope it helps someone.

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