Jdbctemplate查询:EmptyResultDataAccessException:结果大小不正确:预期为1,实际为0

时间:2015-08-30 20:11:11

标签: java spring-data jdbctemplate

我正在使用Jdbctemplate来检索' Spittles'来自db。这是我的方法

private static final String SQL_SELECT_SPITTLE =
                    "select id, spitter_id, spittleText, postedTime from spittle";

public List<Spittle> getRecentSpittles() {
                return jdbcTemplate.query(SQL_SELECT_SPITTLE, new RowMapper<Spittle>() {

    public Spittle mapRow(ResultSet resultSet, int i) throws SQLException {
                        Spittle spittle = new Spittle();
                        spittle.setId(resultSet.getLong(1));
                        spittle.setSpitter(getSpitterById(resultSet.getLong(2)));
                        spittle.setText(resultSet.getString(3));
                        spittle.setWhen(resultSet.getDate(4));
                        return spittle;
                    }
                });
            }

以下是我的数据库表

  id  spitter_id spittleText           postedTime
  1       0        Hello               2015-08-24
  2       4        I've been in London!2015-08-25
  3       3        I'm glad            2015-08-28

这是我的堆栈跟踪

INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1b8d17c: startup date [Sun Aug 30 22:36:35 CEST 2015]; root of context hierarchy
sie 30, 2015 10:36:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'jdbcTemplateSpitterDao' with a different definition: replacing [Generic bean: class [com.gnology.springdataaccess.implementations.JdbcTemplateSpitterDao]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Damian\jdbc-project\target\classes\com\gnology\springdataaccess\implementations\JdbcTemplateSpitterDao.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfiguration; factoryMethodName=jdbcTemplateSpitterDao; initMethodName=null; destroyMethodName=(inferred); defined in com.gnology.springdataaccess.AppConfiguration]
sie 30, 2015 10:36:36 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Exception in thread "main" org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
    at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:797)
    at com.gnology.springdataaccess.implementations.JdbcTemplateSpitterDao.getSpitterById(JdbcTemplateSpitterDao.java:74)
    at com.gnology.springdataaccess.implementations.JdbcTemplateSpitterDao$2.mapRow(JdbcTemplateSpitterDao.java:94)
    at com.gnology.springdataaccess.implementations.JdbcTemplateSpitterDao$2.mapRow(JdbcTemplateSpitterDao.java:90)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:459)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:480)
    at com.gnology.springdataaccess.implementations.JdbcTemplateSpitterDao.getRecentSpittles(JdbcTemplateSpitterDao.java:90)
    at com.gnology.springdataaccess.implementations.SpitterServiceImpl.getRecentSpittles(SpitterServiceImpl.java:32)
    at com.gnology.springdataaccess.Main.main(Main.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

昨天我没有遇到任何问题,我不知道如何处理这个错误。 我怎样才能解决这个问题?提前谢谢。

0 个答案:

没有答案