Spring Data JDBC Extensions

org.springframework.data.jdbc.core
Class OneToManyResultSetExtractor<R,C,K>

java.lang.Object
  extended by org.springframework.data.jdbc.core.OneToManyResultSetExtractor<R,C,K>
All Implemented Interfaces:
ResultSetExtractor<List<R>>

public abstract class OneToManyResultSetExtractor<R,C,K>
extends Object
implements ResultSetExtractor<List<R>>

An abstract results extractor for row mapping operations that map multiple rows to a single root object. This is useful when joining a one-to-many relationship where there can be multiple child rows returned per parent root.

It's assumed that the root type R table has a primary key (id) of type K and that the child type C table has a foreign key of type K referencing the root table's primary key.

For example, consider the relationship: "a Customer has one-to-many Addresses". When joining the Customer table with the Address table to build a Customer object, multiple rows would be returned for a Customer if it has more than one Address. This extractor is useful in that case.

Since:
1.0
Author:
Thomas Risberg, Keith Donald, Oliver Gierke

Nested Class Summary
static class OneToManyResultSetExtractor.ExpectedResults
           
 
Field Summary
protected  RowMapper<C> childMapper
           
protected  OneToManyResultSetExtractor.ExpectedResults expectedResults
           
protected  List<R> results
           
protected  RowMapper<R> rootMapper
           
 
Constructor Summary
OneToManyResultSetExtractor(RowMapper<R> rootMapper, RowMapper<C> childMapper)
          Creates a new OneToManyResultSetExtractor from the given RowMappers.
OneToManyResultSetExtractor(RowMapper<R> rootMapper, RowMapper<C> childMapper, OneToManyResultSetExtractor.ExpectedResults expectedResults)
          Creates a new OneToManyResultSetExtractor from the given RowMappers and OneToManyResultSetExtractor.ExpectedResults.
 
Method Summary
protected abstract  void addChild(R root, C child)
          Add the child object to the root object This method must be implemented by subclasses.
 List<R> extractData(ResultSet rs)
           
protected abstract  K mapForeignKey(ResultSet rs)
          Map the foreign key value to the required type.
protected abstract  K mapPrimaryKey(ResultSet rs)
          Map the primary key value to the required type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expectedResults

protected final OneToManyResultSetExtractor.ExpectedResults expectedResults

rootMapper

protected final RowMapper<R> rootMapper

childMapper

protected final RowMapper<C> childMapper

results

protected List<R> results
Constructor Detail

OneToManyResultSetExtractor

public OneToManyResultSetExtractor(RowMapper<R> rootMapper,
                                   RowMapper<C> childMapper)
Creates a new OneToManyResultSetExtractor from the given RowMappers.

Parameters:
rootMapper - RowMapper to map the root entity, must not be null.
childMapper - RowMapper to map the root entities, must not be null.

OneToManyResultSetExtractor

public OneToManyResultSetExtractor(RowMapper<R> rootMapper,
                                   RowMapper<C> childMapper,
                                   OneToManyResultSetExtractor.ExpectedResults expectedResults)
Creates a new OneToManyResultSetExtractor from the given RowMappers and OneToManyResultSetExtractor.ExpectedResults.

Parameters:
rootMapper - RowMapper to map the root entity, must not be null.
childMapper - RowMapper to map the root entities, must not be null.
expectedResults -
Method Detail

extractData

public List<R> extractData(ResultSet rs)
                    throws SQLException,
                           DataAccessException
Specified by:
extractData in interface ResultSetExtractor<List<R>>
Throws:
SQLException
DataAccessException

mapPrimaryKey

protected abstract K mapPrimaryKey(ResultSet rs)
                            throws SQLException
Map the primary key value to the required type. This method must be implemented by subclasses. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

Parameters:
rs - the ResultSet
Returns:
the primary key value
Throws:
SQLException

mapForeignKey

protected abstract K mapForeignKey(ResultSet rs)
                            throws SQLException
Map the foreign key value to the required type. This method must be implemented by subclasses. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

Parameters:
rs - the ResultSet
Returns:
the foreign key value
Throws:
SQLException

addChild

protected abstract void addChild(R root,
                                 C child)
Add the child object to the root object This method must be implemented by subclasses.

Parameters:
root - the Root object
child - the Child object

Spring Data JDBC Extensions