title: Spring Jdbctemplate one to many data retrieving
body: <p>I'm working in a spring jdbc template project. There I want to load all the records from an one to many relationship. Following is the my database structure, </p>
  
  <pre><code>class ReceiptHeader{
   int rcptid,
   String rcptname;
   List&lt;ReceiptDetails&gt; receiptDetails;
  }
  
  class ReceiptDetails{
   int detid;
   String comment;
  }
  </code></pre>
  
  <p>I want to load all the records in ReceiptHeader to a list&lt;> and all the ReceiptDetails records must attached to it.</p>
  
  <p>My code is,</p>
  
  <pre><code>@Override   
      public &lt;E, U, V&gt; List&lt;E&gt; getData(U param, V invId) throws Exception {
          List&lt;ReceiptHdr&gt; result = null;
  
          try {
              logger.info("--------Excute getData --&gt;");
              JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
  
              final Map&lt;Integer, ReceiptHdr&gt; rcptHdr = new HashMap&lt;Integer, ReceiptHdr&gt;();
  
              jdbcTemplate.query("select h.rcpt_id, h.remark, d.det_id, d.comment from receipt_hdr h inner join receipt_det on h.rcpt_id = d.rcpt_id", new RowMapper&lt;ReceiptDet&gt;() {
                  public ReceiptHdr mapRow(ResultSet rs, int rowNum) throws SQLException {
                      Integer rcptId = rs.getInt("rcpt_id");
                      ReceiptHdr rcptHeader = (ReceiptHdr) rcptHdr.get(rcptId);
                      if (rcptHeader == null) {
                          String hdrRemark = rs.getString("remark");
                          rcptHeader = new ReceiptHdr();
                          rcptHeader.setRcptId(rcptId);
                          rcptHeader.setHdrRemark(hdrRemark);
                          rcptHdr.put(rcptId, rcptHeader);
                      }
                      ReceiptDet rcptDet = new ReceiptDet();
                      rcptDet.setRcptDetId(rs.getInt("det_id"));
                      rcptDet.setDeleteComment(rs.getString("comment"));
                      rcptHeader.getReceiptDets().add(rcptDet);
                      return rcptDet;
                  }
              });
              result = new ArrayList&lt;ReceiptHdr&gt;(rcptHdr.values());
  
              logger.info("--------Excute ReceiptHdr --&gt;" + rcptHdr);
  
          } catch (Exception e) {
              logger.error("Error : Get Data--&gt;" + e);
          }
          return (List&lt;E&gt;) result;
      }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/ltunE.jpg" rel="nofollow">The screen shot of the code is here</a></p>
  
  <p>I tried following two methods, 
  but unsuccessfull,
  <a href="http://www.hitmaroc.net/680540-7774-using-spring-jdbc-template-populating-results.html" rel="nofollow">this link</a></p>