Search code examples
javasqljavabeansjdbi

Does JDBI (3) have asymmetric support for beans?


JDBI (3) has fantastic support for querying a bean:

@SqlQuery("SELECT * FROM user ORDER BY name")
@RegisterBeanMapper(User.class)
List<User> listUsers();

And, assuming the compiler is set to include parameter names, it also supports inserting beans:

@SqlUpdate("INSERT INTO user(id, name) VALUES (:id, :name)")
void insertBean(@BindBean User user);

But apparently, when inserting a bean, it seems you need to enumerate all the bean fields, whereas for querying one, JDBI can figure them out on its own. Am I missing a solution, or does JDBI really have such different capabilities for querying/inserting ?

I would think that for both directions, the same information about the bean are needed, so I don't understand why for inserting, it can't figure out the fields on its own. Is there a reason for this difference, or am I just not seeing something obvious ?


Solution

  • Jdbi is intended as a convenience layer to make it easier to work with SQL databases in Java.

    Give Jdbi a SQL statement and some parameters, and the library will execute it and stuff the result (if any) into some object for you--the end.

    Generating SQL is not a goal of Jdbi, although we do provide a few small conveniences through templating, e.g. @BindList.

    From the project documentation (emphasis added):

    Jdbi is not an ORM. There is no session cache, change tracking, "open session in view", or cajoling the library to understand your schema.

    Instead, Jdbi provides straightforward mapping between SQL and simple tabular data structures.

    You bring your own SQL, and Jdbi only runs the commands you tell it to—​the way God intended.