Search code examples
javamysqljdbi

JDBI Object Query


I've used JDBI before for Java persistence stuff before but it's always been the fluent API not the object API. Trying the Object API now.

I've got a DAO Object that is pretty simple:

public interface PersonDAO {

@SqlQuery("insert into person(id,first_name,last_name,position) values(:id,:firstName,:lastName,:position)")
void insertPerson(@Bind("id") Integer id,
                  @Bind("firstName") String firstName,
                  @Bind("lastName") String lastName,
                  @Bind("position") String position);
}

Tested the query in mysql, it works fine, but running it in a unit test:

@Test 
public void testInsertPerson() {
    PersonDAO personDao = dao.getRegHandle().attach(PersonDAO.class);
    personDao.insertPerson(888888,"Tom", "Ford", "Manager");
}

I get an exception:

java.lang.IllegalStateException: Method com.hrweb.dao.PersonDAO#insertPerson is annotated as if it should return a value, but the method is void.

What am I doing wrong here?


Solution

  • It looks like you've got the SqlQuery annotation not the SqlUpdate annotation on your insert statement. Take a look at examples in the JDBI Docs: Link

    Change the annotation like this:

    @SqlUpdate("insert into person(id,first_name,last_name,position) values(:id,:firstName,:lastName,:position)")
    

    Should get rid of that exception.