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
You can simply use this library - SimpleFlatMapper has already solved that problem. All you need to do is create a ResultSetExtractor using the JdbcTemplateMapperFactory.
import org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory;
private final ResultSetExtractor> resultSetExtractor =
JdbcTemplateMapperFactory
.newInstance()
.addKeys("id") // the column name you expect the invoice id to be on
.newResultSetExtractor(Invoice.class);
String query = "SELECT * FROM INVOICE inv JOIN INVOICE_LINE line on inv.id = line.invoice_id"
List results = jdbcTemplate.query(query, resultSetExtractor);
Add this dependancey to the pom.xml
org.simpleflatmapper
sfm-springjdbc
8.2.1
Here is an article to refer - https://arnaudroger.github.io/blog/2017/06/13/jdbc-template-one-to-many.html
Here are examples for different usages - https://github.com/arnaudroger/SimpleFlatMapper/blob/master/sfm-springjdbc/src/test/java/org/simpleflatmapper/jdbc/spring/test/JdbcTemplateMapperFactoryTest.java