I am using Apache Commons DBUtils according to QueryRunner#insert method in its documentation, insert return the generic type of ResultSetHandler. I have a BR_Author object for my project.
BR_Author.java
import org.springframework.stereotype.Repository;
@Repository
public class BR_Author {
private int id;
private String authorName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
I write a simple insert statement at my service class like
AuthorService#createAuthor
public BR_Author createAuthor(String authorName) throws ApiException {
String sql = "Insert into BR_AUTHOR(authorName) VALUES(?)";
ResultSetHandler<BR_Author> rs = new BeanHandler<BR_Author>(
BR_Author.class);
QueryRunner qr = new QueryRunner(dataSource);
Object[] params = { authorName };
try {
BR_Author author = qr.insert(sql, rs, params);
System.out.println("Br_Author:" + author);
return author;
} catch (SQLException e) {
throw new ApiException(ErrorCode.ERR20001, e.getMessage());
}
}
I am trying to return the added value at the createAuthor method, but if i configure the id field as auto-increment object return as
Br_Author:Br_Author [id=0, authorName=null]
when I check the db I see that it adds the values successfully.
If I disable auto-increment and set the id from the code, the author object is null. So I want to learn that am I misunderstood the QueryRunner#insert method or if it has a bug. I already checked the below links.
BTW: Select queries working fine for the BR_Author class so it means there shouldn't be any mapping issue.
I didn't find any official solution from DBUtils API so i just return generated ID and then query it after insert the record with this id at Service layer. This way method return Object at service layer, but i do one extra query.