Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JDBC RowMapper usage for eager fetches

The question is about the best practice usage for RowMapper in master/detail scenarios where we want to eagerly fetch details using spring jdbc.

Assume that we have both Invoice and InvoiceLine classes.

public class Invoice{
    private BigDecimal invId;
    private Date invDate;
    private List<InvoiceLine> lines;
}
public class InvoiceLine{
    private int order;
    private BigDecimal price;
    private BigDecimal quantity;
}

When using Spring Jdbc with a row mapper we usually have a

public class InvoiceMapper implements RowMapper<Invoice>{
    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;
    }
}

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.

public class InvoiceMapper implements RowMapper<Invoice>{
    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;
    }
}

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.

like image 599
Serkan Arıkuşu Avatar asked Jul 12 '12 15:07

Serkan Arıkuşu


People also ask

What is the use of RowMapper in Spring JDBC?

In Spring, the RowMapper interface is used to fetch the records from the database using the query() method of the JdbcTemplate class. RowMapper is a callback interface that is called for each row and maps the row of relations with the instances to the model(user-defined) class.

What is the purpose of RowMapper interface?

RowMapper<T> interface is used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object. SQLExceptions if any thrown will be caught and handled by the calling JdbcTemplate.

What is the difference between ResultSetExtractor and RowMapper?

ResultSetExtractor is suppose to extract the whole ResultSet (possibly multiple rows), while RowMapper is feeded with row at a time. Most the time, ResultSetExtractor will loop the ResultSet and use RowMapper , snippet example of Spring RowMapperResultSetExtractor : List<T> results = (this.

What is Bean property RowMapper?

BeanPropertyRowMapper is a RowMapper implementation that converts a table row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.


1 Answers

The ResultSetExtractor is a better option for doing this. Execute one query that joins both the tables and then iterate through the result set. You will need to have some logic to aggregate multiple rows belonging to the same invoice - either by ordering by invoice id and checking when the id changes or using a map like shown in the example below.

jdbcTemplate.query("SELECT * FROM INVOICE inv JOIN INVOICE_LINE line " +
   + " on inv.id = line.invoice_id", new ResultSetExtractor<List<Invoice>>() {

    public List<Invoice> extractData(ResultSet rs) {
        Map<Integer,Invoice> invoices = new HashMap<Integer,Invoice>();
        while(rs.hasNext()) {
            rs.next();
            Integer invoiceId = rs.getInt("inv.id");
            Invoice invoice = invoces.get(invoiceId);
            if (invoice == null) {
               invoice = invoiceRowMapper.mapRow(rs);
               invoices.put(invoiceId,invoice);
            }
            InvoiceItem item = invLineMapper.mapRow(rs);
            invoice.addItem(item);  
        }
        return invoices.values();
    }


});
like image 96
gkamal Avatar answered Sep 23 '22 00:09

gkamal