0

My database contains many columns where booleans are represented by a 0 or a 1. I'd like to convert these to booleans when they are mapped from the database while retaining the ability to use @RegisterBeanMapper(Dog.class) for most of the other fields.

I'm wondering if there's some way to use the standard bean mapper, but apply a custom mapper on a per field basis, like this:

class Dog {
    String name;
    int legCount;
    @Mapper(BooltegerMapper.class)
    boolean isSnifferous;
}

Most columns would be automagically mapped, while isSnifferous would use the BooltegerMapper that converts an Integer to boolean.

Tremelune
  • 352
  • 2
  • 11
  • When you use: `@RegisterBeanMapper(Dog.class)` – Tremelune Aug 17 '18 at 16:29
  • 1
    Jdbi project member here. If _all_ booleans in your database are stored as numbers, then you can simply register a mapper that converts the number to boolean. Any built-in mapper can be overridden simply by registering a new mapper for the already-supported type. Last-registered mapper wins for any given type. – qualidafial Aug 28 '18 at 16:29
  • If you have a max of true booleans and numeric booleans, your mapper could use `ResultSet.getMetaData().getColumnType(columnNumber)` to differentiate between them and fetch the value according to the column type. – qualidafial Aug 28 '18 at 16:31

0 Answers0