title: Spring JDBC RowMapper usage for eager fetches
body: <p>The question is about the best practice usage for RowMapper in master/detail scenarios where we want to eagerly fetch details using spring jdbc.</p>
  
  <p>Assume that we have both Invoice and InvoiceLine classes.</p>
  
  <pre><code>public class Invoice{
      private BigDecimal invId;
      private Date invDate;
      private List&lt;InvoiceLine&gt; lines;
  }
  public class InvoiceLine{
      private int order;
      private BigDecimal price;
      private BigDecimal quantity;
  }
  </code></pre>
  
  <p>When using Spring Jdbc with a row mapper we usually have a</p>
  
  <pre><code>public class InvoiceMapper implements RowMapper&lt;Invoice&gt;{
      public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException {
           Invoice invoice = new Invoice();
           invoice.setInvId(rs.getBigDecimal("INVID"));
           invoice.setInvDate(rs.getDate("INVDATE"));
           return invoice;
      }
  }
  </code></pre>
  
  <p>Now the problem is I want to eagerly fetch InvoiceLine's related with this invoice instance.
  Would it be OK if I query database in the rowmapper class? Or anyone prefers another way? I use the pattern below but not happy with that.</p>
  
  <pre><code>public class InvoiceMapper implements RowMapper&lt;Invoice&gt;{
      private JdbcTemplate jdbcTemplate;
      private static final String SQLINVLINE=
              "SELECT * FROM INVOICELINES WHERE INVID = ?";
  
      public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException {
           Invoice invoice = new Invoice();
           invoice.setInvId(rs.getBigDecimal("INVID"));
           invoice.setInvDate(rs.getDate("INVDATE"));
           invoice.setLines(jdbcTemplate.query(SQLINVLINE, 
                            new Object[]{invoice.getInvId},new InvLineMapper());
  
           return invoice;
      }
  }
  </code></pre>
  
  <p>I sense that something is wrong with this approach but could not get a better way. I would be more than glad if someone can show me why is this a bad design and if so what would be the correct usage.</p>