title: How to retrieve a row in a database and convert it into an object in Spring?
body: <p>I'm trying to learn how to do CRUD operations in Spring 2.5.6. I made a table in my database called companies that has 2 fields: id and name. What I want to do is to retrieve a row from the table using the id field. This is what I made for it.</p>
  
  <pre><code>public class JdbcCompanyDao extends SimpleJdbcDaoSupport implements CompanyDao {
      protected final Log logger = LogFactory.getLog(getClass());
  
      public Company getCompany(int id) {
          logger.info("Getting company with id = " + id);
          Company company = getSimpleJdbcTemplate().queryForObject(
                  "SELECT id, name FROM companies WHERE id = " + id,
                  new CompanyMapper());
          return company;
      }
  
      private static class CompanyMapper implements ParameterizedRowMapper&lt;Company&gt; {
          public Company mapRow(ResultSet rs, int rowNum) throws SQLException {
              Company company = new Company();
              company.setId(rs.getInt("id"));
              company.setName(rs.getString("name"));
              return company;
          }
      }
  }
  </code></pre>
  
  <p>I made a unit test for it to check if I did it right:</p>
  
  <pre><code>public class JdbcCompanyDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
      private CompanyDao companyDao;
  
      public void setCompanyDao(CompanyDao companyDao) {
          this.companyDao = companyDao;
      }
  
      @Override
      protected String[] getConfigLocations() {
          return new String[] {"classpath:test-context.xml"};
      }
  
      @Override
      protected void onSetUpInTransaction() throws Exception {
          super.deleteFromTables(new String[] {"companies"});
          super.executeSqlScript("file:db/load_data.sql", true);
      }
  
      public void testGetCompany() {
          Company company = companyDao.getCompany(1);
  
          assertEquals("SomeRandomCompany", company.getName());
      }
  }
  </code></pre>
  
  <p>When I run the test, I get the following error:</p>
  
  <blockquote>
    <p>org.springframework.dao.EmptyResultDataAccessException:
    Incorrect result size: expected 1,
    actual 0</p>
  </blockquote>
  
  <p>I can't find the reason why <em>getCompany()</em> is returning an empty result set. Right now, I have no idea what I need to do and I'm still confused about how Spring works. Does it have something to do with how <em>AbstractTransactionalDataSourceSpringContextTests</em> works?</p>
  
  <p>Side question: Are there any good resources that demonstrates how to do CRUD operations in Spring? So far, the only resources I have are <a href="http://static.springsource.org/docs/Spring-MVC-step-by-step/index.html" rel="nofollow">this</a> and <a href="http://maestric.com/doc/java/spring" rel="nofollow">this</a> and they don't really provide enough examples for me get anything going on.</p>