A repository method issues a single SQL query that joins ORDERS to ORDER_ITEMS, so an order with three line items comes back as three rows that share the same order id. The method must return one fully populated `Order` object whose `items` collection holds all three line items, and the `JdbcTemplate` query call itself must produce that `Order` as its return value. Which `JdbcTemplate` callback interface is designed for this job, and why?
A. `RowMapper`, because Spring invokes it once per row and the mapper can advance the `ResultSet` itself to pull in the remaining rows for the same order
Misconception that a RowMapper may drive the cursor. `RowMapper.mapRow` is called once per row by the template, must map only the current row, and must not call `next()` on the ResultSet; a `query` with a RowMapper also returns a `List` of per-row objects, not one aggregate object.
B. `RowCallbackHandler`, because it is the only callback that can accumulate state across rows and hand the finished object back as the query's return value
Confuses stateful row processing with producing a return value. `RowCallbackHandler.processRow` returns `void` and the corresponding `JdbcTemplate.query` overload also returns `void`, so the result can only be dug out of the handler afterwards — it cannot be the query call's return value. It is intended for streaming side effects (writing a file, accumulating a running total) rather than building a returned object graph.
C. `ResultSetExtractor`, because it is handed the entire `ResultSet` once and is responsible for iterating it, so it can fold many rows into a single returned objectCorrect answer
Correct. `ResultSetExtractor.extractData` is invoked exactly once with the whole `ResultSet`; the implementation owns the iteration and returns an arbitrary object of type `T`, which `JdbcTemplate.query` returns directly. That is precisely the documented use case for mapping a one-to-many join into one aggregate root.
D. `PreparedStatementCallback`, because collapsing multiple rows into one object requires working below the row-mapping layer, directly on the JDBC statement
Confuses the low-level `execute` callbacks with result-set extraction. `PreparedStatementCallback` (like `ConnectionCallback`) is passed to `JdbcTemplate.execute` and gives you the raw `PreparedStatement` for operations the query/update API does not cover; the ordinary query API already exposes whole-ResultSet access, so dropping to this level is unnecessary here.
Explanation
Spring's JDBC query callbacks differ in how many times the template invokes them and in what the call returns. `ResultSetExtractor` is called once with the entire `ResultSet`, makes the implementation responsible for iterating, and returns any object the implementation builds — which is what lets rows from a one-to-many join be folded into a single aggregate that `query` then returns. A per-row mapper is called once per row and is contractually forbidden from moving the cursor, so it cannot consume sibling rows and yields a list rather than one object. A void row-processing handler can hold state but returns nothing from the query call, making it a fit for streaming side effects instead. The statement-level callbacks belong to the `execute` API for operations the query API cannot express, which is not the case here (Spring Framework Reference — Data Access: JdbcTemplate; `ResultSetExtractor` and `RowMapper` javadoc).