没有PreparedStatementCreator,JdbcTemplate的更新方法不会更新

时间:2014-07-21 02:53:35

标签: spring spring-3 spring-jdbc jdbctemplate

我有下表

CREATE TABLE user_use_case_control (
id integer GENERATED BY DEFAULT AS IDENTITY (START WITH 10, INCREMENT BY 1) not null,
name varchar(20) not null,
start_date timestamp not null,
end_date timestamp null,
duration timestamp null,
status varchar(20) null,
user_id varchar(20) not null ); 

我正在使用Spring Framework 4.0.5, JdbcTemplate ,我有以下插入方法(方法的一部分)

α

this.jdbcTemplate.update(

        new PreparedStatementCreator()  {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps = connection.prepareStatement(sql,  
                Statement.RETURN_GENERATED_KEYS);

                ps.setString(1, userControl.getName());
                ps.setTimestamp(2, new Timestamp( userControl.getStartDate().getTime() ));
                ps.setTimestamp(3, new Timestamp( userControl.getEndDate().getTime() ));
                ps.setTimestamp(4, new Timestamp( userControl.getDuration().getTime() ));
                ps.setString(5, userControl.getStatus());
                ps.setString(6, userControl.getUserId());

           return ps;
          }
    }, keyHolder);

观察:

  • 我正在使用PreparedStatementCreator来获取生成的密钥。
  • 我正在使用ps.setTimestampnew Timestamp

插入方法正常。

问题在于更新。

我有以下内容:

β

@Override
public UserControl update(UserControl userControl) {

    logger.info("UserControlJdbcRepository - update {}", userControl.toString());

    String sql = "UPDATE user_use_case_control uucc " +
                 "SET uucc.name=?, uucc.start_date=?, uucc.end_date=?, uucc.duration=?, uucc.status=? " +
                 "WHERE uucc.id=?";


    int result = this.jdbcTemplate.update(sql,
                    userControl.getName(),
                    new Timestamp( userControl.getStartDate().getTime() ),
                    new Timestamp( userControl.getEndDate().getTime() ),
                    new Timestamp( userControl.getDuration().getTime() ),
                    userControl.getStatus(),
                    userControl.getUserId());

    logger.info("result {}", result);
…
  • 使用logger我可以看到对象及其所有变量都不为空

问题是:jdbcTemplate.update 不会更新任何内容!

如果我使用

,情况相同
int result = this.jdbcTemplate.update(sql,
                 userControl.getName(),
                 userControl.getStartDate(),
                 userControl.getEndDate(),
                 userControl.getDuration(),
                 userControl.getStatus(),
                 userControl.getUserId());

如果我使用DEBUG级别执行调试和记录器,我可以看到以下内容:

Executing prepared SQL update
Executing prepared SQL statement [UPDATE user_use_case_control uucc SET uucc.name=?, uucc.start_date=?, uucc.end_date=?, uucc.duration=?, uucc.status=? WHERE uucc.id=?]
SQL update affected 0 rows
SQLWarning ignored: SQL state '02000', error code '-1100', message [no data]

我不明白为什么会发生这种情况。

如果记录器有INFO级别,则不会出现任何错误,我的意思是不会出现一些异常。

如果我使用以下方法更改方法:

奥米克

int result = this.jdbcTemplate.update(

        new PreparedStatementCreator()  {

            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps = connection.prepareStatement(sql);

                ps.setString(1, userControl.getName());
                ps.setTimestamp(2, new Timestamp( userControl.getStartDate().getTime() ));
                ps.setTimestamp(3, new Timestamp( userControl.getEndDate().getTime() ));
                ps.setTimestamp(4, new Timestamp( userControl.getDuration().getTime() ));
                ps.setString(5, userControl.getStatus());
                ps.setInt(6, userControl.getId());

                return ps;
            }

        });

logger.info("result {}", result);

就在那时:它完全正常。

观察:

  • Alpha,Beta和Omicron使用ps.setTimestampnew Timestamp

问题:

  1. 为什么 Beta 失败? (消息[无数据])
  2. 为什么只使用PreparedStatementCreator进行更新?
  3. 注意:我有其他存储库和基于测试版update方法,在没有PreparedStatementCreator的情况下工作,工作正常。

    只是为了突出其他存储库:

    • 其中一些没有timestamp字段,只在db表架构中有date
    • 其中一些在db表架构中既没有timestamp字段也没有date字段。

    因此,当涉及timestamp字段时,这种奇怪的行为仅出现在更新中。

    提前致谢。

0 个答案:

没有答案
相关问题