JDBI3 reverse of AbstractArgumentFactory.build

Viewed 183

My MySQL table looks have three columns one long(user_id) another varchar(name) and finally varchar(rules(this is a JSON array as string)). I have integrated JDBI3 recently and I am using its @BindBean option. My bean(user class) looks as follows.

public class User {
    private long id;
    private String name;
    private List<Rule> rules;
}

and my rule class looks as follow.

public class Rule {
    private long id;
}

I have also created an AbstractArgumentFactory as follows

public class RuleArgumentFactory extends AbstractArgumentFactory<List<Rule>> {

    public RuleArgumentFactory() {
        super(Types.VARCHAR);
    }

    @Override
    protected Argument build(List<Rule> value, ConfigRegistry config) {
        return (position, statement, ctx) -> statement.setString(position, valueToJson());
    }
}

I have finally attached the ArgumentFactory to handle while performing DB queries. Now when I am doing an update or insert operation like as follows it works well.

this.jdbi.useHandle(handle -> {
    handle.registerArgument(new RuleArgumentFactory());
    handle.createUpdate(UPDATE_QUERY)
        .bindBean(User)
        .execute();
});

But the problem is when I perform a select query,

return this.jdbi.withHandle(handle -> {
    handle.registerArgument(new RuleArgumentFactory());
    return handle.createQuery(SELECT_QUERY)
            .bind(param, paramValue)
            .mapToBean(User.class)
            .findFirst();
});

I am unable to get the User Object because when JDBI tries to execute the build function. Arguments to the function gets mismatched as they become (String, ConfigRegistry) while it is accepting (List<Rule>, ConfigRegistry) finally throwing the error argument type mismatch. Is there a way where I can serialise the JSON to the List<Rule> object. Perhaps something opposite to build().

0 Answers
Related