Use BeanMapper from within a RowMapper?

Tags: ,

I’m using JDBI’s SQL Objects declarative API to map an object containing a one-to-many relationship:

class Foo {
  private String id;
  private String name;
  private Set<Bar> bars = new HashSet<>();

class Bar {
  private String id;

Initially it looked like a RowReducer would be ideal:

class FooQuery {

  @RegisterBeanMapper(value = Foo.class, prefix = "f")
  @RegisterBeanMapper(value = Bar.class, prefix = "b")
  ResultIterator<Foo> queryAll();

  static class RowReducer implements LinkedHashMapRowReducer<String, Foo> {

    public void accumulate(Map<String, Foo> map, RowView rowView) {
      final Foo foo = map.computeIfAbsent(rowView.getColumn("f_id", String.class),
          id -> rowView.getRow(Foo.class));
      if (rowView.getColumn("b_id", String.class) != null) {

However I soon discovered that RowReducers don’t work with ResultIterators (I’m working with a large database so it’s important to be able to stream these) so now I’m reverting back to implementing a RowMapper instead. I’d still like to use the handy BeanMappers built into JDBI though but I can’t figure out how to access them from within my RowMapper implementation.

class FooRowMapper implements RowMapper<Foo> {
  private Foo foo = null;

  public Foo map(ResultSet rs, StatementContext ctx) throws SQLException {
    String fooId = rs.getString("f_id");
    if (foo == null || ! {
      // ideally construct using JDBI's BeanMapper similar to how we can above
      // in the RowReducer!
      foo = ??? 
    // same as above...
    Bar bar = ???

    return foo;

Is it possible to easily use BeanMappers from within a RowMapper so I don’t have to manually construct the beans?


RowMapper<Bar> barMapper = BeanMapper.of(Bar.class)
Bar bar =, ctx);

Source: stackoverflow