what is difference between ResultSetExtractor vs Rowmapper? – Spring

Photo of author
Written By M Ibrahim
jdbctemplate spring

Quick Fix: ResultSetExtractor is used to extract the entire ResultSet (possibly multiple rows), while RowMapper is feeded with row at a time. Using ResultSetExtractor can create Out Of Memory exception as ALL results will be transformed.

The Problem:

Explain the key differences between ResultSetExtractor and RowMapper interfaces in Java, including their functionalities, return types, and internal workings.

The Solutions:

Solution 1: Java Spring Framework

The primary distinction between `ResultSetExtractor` and `RowMapper` is in their intended functionality and result handling:

  • **ResultSetExtractor:** Designed to extract data from an entire `ResultSet`, potentially encompassing multiple rows. It returns an object representing the entire result set.
  • **RowMapper:** Processes data on a per-row basis, mapping each row to an individual result object. It typically returns a list or array of these individual objects.

Internally, `ResultSetExtractor` may utilize a `RowMapper` to map individual rows within the `ResultSet`, but its primary goal is to aggregate and process the entire result set. In contrast, `RowMapper` focuses solely on mapping individual rows.

It’s important to note that `ResultSetExtractor` can potentially lead to memory issues if the result set is large, as it processes and stores all rows at once. `RowMapper`, by handling rows one at a time, avoids this potential pitfall.

Solution 2: Difference between ResultSetExtractor vs RowMapper

ResultSetExtractor:

  • Provides processing of the entire ResultSet at once, allowing you to iterate through the results manually.
  • Designed for scenarios where complex operations are required on multiple rows and you need full control over the ResultSet.
  • Example: Joining multiple tables and extracting a complex object from the combined results.

RowMapper:

  • Can be used for both processing individual rows or entire rows.
  • Individual Rows: Maps each row of the ResultSet to a specific object type.
  • Entire Rows: Used with the jdbcTemplate.query() method to return a list of objects representing all rows in the ResultSet.
  • Example: Mapping each employee row in a database table to an Employee object.

Best Use Cases:

  • Row Mapper: When mapping individual rows to domain objects or using private inner classes for mapping.
  • RowCallbackHandler: When no return value is required for each row and operations like writing to a file or filtering rows are performed.
  • ResultSetExtractor: When complex mappings across multiple rows are required or full control over the ResultSet is necessary.
  • ParameterizedRowMapper: Creating complex objects from multiple rows or tables.

Solution 3: Difference between ResultSetExtractor and RowMapper

In addition to the difference you mentioned, here are some key differences between `ResultSetExtractor` and `RowMapper`:

  • `ResultSetExtractor` is used to extract a single object from a `ResultSet`, while `RowMapper` is used to extract a list of objects from a `ResultSet`.
  • `ResultSetExtractor` is more efficient than `RowMapper` because it only iterates over the `ResultSet` once, while `RowMapper` iterates over the `ResultSet` multiple times.
  • `ResultSetExtractor` is more flexible than `RowMapper` because it allows you to extract any type of object from a `ResultSet`, while `RowMapper` only allows you to extract a list of objects that implement the `Map` interface.

Internally, `RowMapper` uses a `ResultSetMetaData` object to determine the number of columns in the `ResultSet` and the data type of each column. It then uses this information to create an instance of the appropriate class for each row in the `ResultSet`. The list of objects created by `RowMapper` is then returned to the caller.

Solution 4:

Another distinction between ResultSetExtractor and RowMapper is that ResultSetExtractor provides control over result processing by allowing you to navigate through all the rows in the result set and return a single object as the result. In contrast, RowMapper processes each row independently and returns a list of objects.

Internally, RowMapper utilizes reflection to create a new instance of the specified object type for each row in the result set. It then uses the values from the result set to populate the properties of the object. Finally, it returns a list of these populated objects.