Skip to content
Advertisement

NoSuchMethodException in JDBI while using it with Lombok

I has immutable class like:

@Value
public class MyEntry {
    int id;
    String name;
}

JDBI Developer Guide states that it should work with @Value annotations. At least with bindBean() or @BindBean.

But when I use org.jdbi.v3.core.result.ResultBearing#mapToBean method with MyEntry.class it throws java.lang.NoSuchMethodException: no such constructor: my.company.MyEntry.<init>()void/newInvokeSpecial. Looks like it trying create object with empty constructor and set fields after it. But I want to let my class be immutable. Any thoughts?

UPD:

lombok version: 1.18.20 jdbi version: 3.25.0 jdk: 15 and 16

Failed code:

return jdbi.withHandle(
    handle -> handle.createQuery("SELECT * FROM MyTable")
        .mapToBean(MyEntry.class)
        .list()
);

Advertisement

Answer

JDBI documentation clearly states:

Use ConstructorMapper or @RegisterConstructorMapper to map @Value classes.

So your code might look like this (but probably there is no need to register row mapper on each call, it could be done on global level):

return jdbi.withHandle(
    handle -> handle.createQuery("SELECT * FROM MyTable")
        .registerRowMapper(ConstructorMapper.factory(MyEntry.class))
        .mapTo(MyEntry.class)
        .list()
);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement