samedi 21 février 2015

Does the rowMappper in spring work for assigning a row to another object?

I have a DAO in my spring project which gets two variables from other tables. I want to fetch a row by ID in this DAO and I want to have those two variables as objects in return. I wrote the DAO class and my rowMapper class as below. But I can not get the author and publisher objects. I am getting nullPointerException. How should I get those objects in my book object(my author and publisher DAO work fine.)?



public class BookDAO extends JdbcDaoSupport {
public Book getBookById(int bookId) throws SQLException,
ClassNotFoundException {
String sql = "select * from library.tbl_book where bookId = ?";
return this.getJdbcTemplate().queryForObject(sql,new Object[] { bookId }, new BookMapper());
}


private static final class BookMapper implements RowMapper<Book> {

@Override
public Book mapRow(ResultSet rs, int rowNum) {
Book book = new Book();
Author author = new Author();
Publisher publisher = new Publisher();
AuthorDAO authorDAO = new AuthorDAO();
PublisherDAO publisherDAO = new PublisherDAO();

try {
book.setBookId(rs.getInt("bookId"));
book.setTitle(rs.getString("title"));
author = authorDAO.getAuthorById(rs.getInt("authId"));
book.setAuthor(author);
publisher = publisherDAO.getPublisherById(rs.getInt("pubId"));
book.setPublisher(publisher);

} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return book;
}

Aucun commentaire:

Enregistrer un commentaire