开发者

Efficiently fill resultset in object model

I have an object model whose structure is

Dashboard

  List of panels

    List of containers

      List of widgets

If i get whole dashboard, with panels + containers + widgets, from Database then multiple I/O requires I want to get it in one I/O .For this i prepared a query which gives me this resultset.

DASHBOARDID   PANELID  CONTAINERID  WIDGETID

13                          11                    5              2

13                          11                    5              3

13                          11                    6              4

13                          11                    6              5

13                          12                    7              6

13                          12                    7              7

13                          12                    8              8

13 开发者_JAVA百科                         12                    8              9

Using list datastructure this model is able to be filled but it takes time i want to efficiently fill this resultset in above object model. is there nay way ?


The strict ordering and nesting in the data makes this fairly straightforward. The solution is a loop that look for changes in ID - when the ID changes a new instance is created. It iteration of the loop maintains the current dashboard, panel, component, widget. For example

Dashboard dashboard,
Panel panel;
Container container;
Widget widget;
List<Dashboard> dashboards; 
ResultSet rs = ...;
while (rs.next())
{
   int dashboardID = rs.getInt(1);
   int panelID = rs.getInt(2);
   int dashboardID = rs.getInt(3);
   int dashboardID = rs.getInt(4);

   if (dashboard==null || dashboardID!=dashboard.id()) {
      dashboard = new Dashboard(dashboardID);
      dashboards.add(dashboard);
   }
   if (panel==null || panelID!=panel.id()) {
      panel = new Panel(panelID);
      dashboard.add(panel);
   }
   if (container!=null || containerID!=container.id()) {
      container = new Container(containerID);
      panel.add(container);
   }
   // wimilarly for widget

}
// all dashboards in "dashboards" initialized with hierarchy


If you are not strictly limited to using JDBC, an ORM solution would resolve your issue. E.g. in Hibernate, you can easily configure your entity mapping to use lazy or eager loading. In the latter case, when you load a Dashboard object, the persistence framework fetches all the contained panels, containers and widgets with 1 to 4 queries total (depending on configuration - note that a single join query for 4 tables might not be optimal if there are many rows), and initialize the whole object graph for you automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜