JDBC ResultSet Mapper

In building a better bean processor I discussed some ideas for reflection mapping of a ResultSet to Object.

This time around we can look at a solution design.


Here is a UML diagram of the solution (click on the image for a bigger, clearer picture).

Note that the diagram contains a couple of example extensions for the DBUtils BeanProcessor and Spring RowCallbackHandler. One of the requirements for the ResultSetMapper is that it can be easily be plugged into these bigger frameworks.

ResultSetMapper UML

Basic Mapping

Data mapping will occur against ResultSet columns and Object fields.

By default the Object field will require the @MapToData annotation, though this requirement may be turned on/off with ResultSetMapper.setAnnotationRequired(true/false). Why have it off? There would be no requirement to add an empty ‘@MapToData’ annotation. So, why have it on? The source code contains a documentary ‘@MapToData’ annotation to indicate that some magic is being performed on the field.

The mapping will use a NameMatcher to compare the field name with the column name (unless a MapToData.columnAlias is specified). A default NameMatcher – NameConverter – will use simple camelCase to under_score matching.

Data mapping will occur, in order of priority:

  1. Via a specified setter procedure – MapToData.setter
  2. Via the JavaBean standard default setter
  3. Directly into the object field


Inheritance requires that many target classes may be supplied to the ResultSetMapper. However, this means that there must be some way of choosing which target to create. To allow selection of target objects, an ObjectValidator will be used – every type of target class will be constructed from the ResultSet and passed to the ObjectValidator. (The default validator always returns true.)

The advantage of this approach is that the programmer only deals in their domain object, but it does require an overhead of creating extra objects for validation.


Solving this problem requires a couple of technical problem answers:

  • How to specify that an Object is an aggregate target (eg MyDataObject), rather than ‘Just Another Object’ (eg String, MyTransientObject)? Answer: MapToData.isAggregateTarget = true/false
  • How to have multiple fields of the same class with different business meanings (eg MyDataObject start; MyDataObject end;)? Answer: MapToData.columnPrefix or MapToData.columnSuffix


So the example domain from the previous post would now be:

public abstract class Jelly { 
  Long jellyId; 
  String jellyType; 
public class JellyAttribute { 
  String name; 
  String value; 
public class JellyCompany {
  String companyName;
  String address;
public class JellyBean extends Jelly { 
  String targetMarket;
  BigDecimal weight;
  @MapToData (columnPrefix="flavour_", isAggregateTarget = true)
  JellyAttribute flavour; 
  @MapToData (columnPrefix="colour_", isAggregateTarget = true)
  JellyAttribute colour; 
  @MapToData (isAggregateTarget = true)
  JellyCompany company;
public class JellyCup extends Jelly {
  String productName;
  @MapToData ( columnAliases = { "cup_volume" } )
  BigDecimal volume;
  @MapToData (isAggregateTarget = true)
  JellyAttribute shape;
public class JellyCupAndSpoon extends JellyCup {
  String spoonMaterial;
  BigDecimal spoonLength;
Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

5 thoughts on “JDBC ResultSet Mapper

  1. Pingback: A Few More Words » ResultSetMapper Project - Map a Java ResultSet to an Object

  2. Norival

    Is the source code available on sourceforge updated?. Because I recompile and it behaves diferently as expected. It difers from the JAR available.


  3. LastFlorencia

    I see you don’t monetize your site, don’t waste your traffic, you can earn extra
    cash every month because you’ve got hi quality content.
    If you want to know how to make extra $$$, search
    for: Mertiso’s tips best adsense alternative

  4. AimeeSmall

    I have checked your site and i’ve found some duplicate content,
    that’s why you don’t rank high in google, but there
    is a tool that can help you to create 100% unique articles, search for: Boorfe’s tips unlimited content


Leave a Reply

Your email address will not be published. Required fields are marked *