Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to transform DBUtils resultset into JavaBeans composited from more domain objects?
    text
    copied!<p>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.</p> <p>With respect to a very few examples I found I created this RowProcessor implementation.</p> <pre><code>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; } } </code></pre> <p><strong>Question:</strong> I would like to know know how to use this row processor in my DAO object and if this implementation is correct?</p> <hr> <p>Commonly I transform rows into JavaBeans in this way:</p> <pre><code>ResultSetHandler&lt;List&lt;MonthOrderCount&gt;&gt; listUrlHandler = new BeanListHandler&lt;&gt;(MonthOrderCount.class); </code></pre> <p>But in my situation first Ineed to create nested objects and then create a final JavaBean, so I assume I need custom row processor.</p> <hr> <p>Structure of my domain objects is:</p> <p>MonthOrderCount class:</p> <pre><code>public class MonthOrderCount { private Month month; private int orderCount; } </code></pre> <p>Month class:</p> <pre><code>public class Month { private Quarter quarter; private Year year; private int monthNo; private String monthName; private String monthAbbreviation; } </code></pre> <p>Quarter class:</p> <pre><code>public class Quarter { private int quarter; private String abbreviation; } </code></pre> <p>Year class:</p> <pre><code>public class Year { private int yearNo; private String year2; private String year4; } </code></pre> <p><strong>EDIT:</strong> 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.</p> <blockquote> <p>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}]</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload