Kragh
Kragh

Reputation: 403

how to combine JDBI @GetGeneratedKeys with Mapper

I'd like JDBI to convert autogenerated primary key (Long value) to another class.

My DAO:

@RegisterMapper(SystemIdMapper.class)
public interface SystemDao {
  @SqlUpdate("insert into systems(device_id, user_id) values(:deviceId.value, :userId.value)")
  @GetGeneratedKeys
  @Mapper(SystemIdMapper.class)
  SystemId insert(@BindBean("deviceId") DeviceId deviceId, @BindBean("userId") UserId userId);

}

My mapper:

public class SystemIdMapper implements ResultSetMapper<SystemId> {
  @Override
  public SystemId map(int index, ResultSet r, StatementContext ctx) {
    return new SystemId(0L); //fake mapping to simplify example
  }
}

When I run my code I got NullPointerException in FigureItOutResultSetMapper.map(..) because

f = factory.mapperFor(rt, ctx);

set f to null. So my guess is that my mapper is incorrectly registered.

Except of using @RegisterMapper and @Mapper(SystemIdMapper.class) annotations I've also tried:

dbi.registerMapper(new SystemIdMapper());

but still with no luck.

Upvotes: 3

Views: 1393

Answers (1)

Manikandan
Manikandan

Reputation: 3165

You need to specify mapper in GetGeneratedKeys annotation.

@GetGeneratedKeys(SystemIdMapper.class)

@Mapper or @RegisterMapper is not used for getting the id back. It is used only during selecting values from table.

Upvotes: 3

Related Questions