Search code examples
javaspring-bootspark-javajdbi

Is @Bind needed?


I discovered by accident that in a spring boot project I didn't have to bind argument in a query like the one below.

@SqlQuery("""
        select id, name
        from organisations
        where id = :id
          """)
@RegisterRowMapper(OrganisationMapper.class)
Organisation getOrgansation(@Bind("id") String id);

This worked:

@SqlQuery("""
        select id, name
        from organisations
        where id = :id
          """)
@RegisterRowMapper(OrganisationMapper.class)
Organisation getOrgansation(String id);

However, when I upgraded another project, not a spring boot project, to use 3.43.0 I could not remove the binding.

Does anyone have a good explanation why I could get queries to work without the binding in a spring boot project but not in a plain java project? (Except for the obvious explanation, magic). Are there any trick I could apply to skip the bindings?


Solution

  • You can only omit the annotation when the code is being compiled with the javac flag -parameters. It's very likely that the first project is being compiled with the flag, and the other is not.

    The flag -parameters has been introduced with Java 8. If it is used, then variable names of methods will be available to reflection during runtime. When JDBI can infer that the variable name was id through reflection, the @Bind annotation is not needed to make it clear that it is meant for the query parameter id.

    Please also have a look at the corresponding section of the reference documentation: https://jdbi.org/#compiling_with_parameter_names