jdbc.update在数据库中成功执行,但keyholder未返回任何内容。

huangapple go评论66阅读模式
英文:

jdbc.update succed in database, but keyholder no content return

问题

这是我的代码:

private long saveTacoInfo(Taco taco) {
    taco.setCreatedAt(new Date());
    PreparedStatementCreator psc =
        new PreparedStatementCreatorFactory(
            "insert into Taco (name, createdAt) values (?, ?)",
            Types.VARCHAR, Types.TIMESTAMP
        ).newPreparedStatementCreator(
            Arrays.asList(
                taco.getName(),
                new Timestamp(taco.getCreatedAt().getTime())));

    KeyHolder keyHolder = new GeneratedKeyHolder();
    int res = jdbc.update(psc, keyHolder);
    log.info("res " + res);
    return keyHolder.getKey().longValue();
}

调试后发现 keyHolder.getKey() 返回 null,因为 GeneratedKeyHolder 的 keyList 为空!
有什么建议吗?

这是我的 Taco 表 SQL:

create table if not exists Taco (
  id identity,
  name varchar(50) not null,
  createdAt timestamp not null
);

插入语句执行成功!H2 数据库中有数据,但 keyHolder.getKey 为 null。

英文:

this is my code

private long saveTacoInfo(Taco taco) {
    taco.setCreatedAt(new Date());
    PreparedStatementCreator psc =
        new PreparedStatementCreatorFactory(
            "insert into Taco (name, createdAt) values (?, ?)",
            Types.VARCHAR, Types.TIMESTAMP
        ).newPreparedStatementCreator(
            Arrays.asList(
                taco.getName(),
                new Timestamp(taco.getCreatedAt().getTime())));

    KeyHolder keyHolder = new GeneratedKeyHolder();
    int res = jdbc.update(psc, keyHolder);
    log.info("res " + res);
    return keyHolder.getKey().longValue();
}

after debugging, found keyHolder.getKey() return null, because GeneratedKeyHolder keyList is empty!
any suggestions?

this is my Taco table sql:

create table if not exists Taco (
  id identity,
  name varchar(50) not null,
  createdAt timestamp not null
);

the Insert statment is execute ok! h2 database has the data. but keyHolder.getKey is null.

答案1

得分: 3

我遇到了与你相同的问题。显然,你使用了《Spring实战》一书中的示例。

看起来问题出在代码中,具体来说是在"PreparedStatementCreatorFactory"类中,"returnGeneratedKeys"字段被设置为false。

我不知道为什么这个示例在书中能够正常工作,或者为什么没有提到需要更改这个变量。根据Github上的信息,这个变量的值自2008年以来没有改变。

以下是类代码的一部分:

public class PreparedStatementCreatorFactory {

    /** The SQL, which won't change when the parameters change. */
    private final String sql;

    /** List of SqlParameter objects (may not be {@code null}). */
    private final List<SqlParameter> declaredParameters;

    private int resultSetType = ResultSet.TYPE_FORWARD_ONLY;

    private boolean updatableResults = false;

    private boolean returnGeneratedKeys = false;

    @Nullable
    private String[] generatedKeysColumnNames;

通过将"returnGeneratedKeys"字段的值设置为true来解决问题。类似这样:

var preparedStatementCreatorFactory = new PreparedStatementCreatorFactory(query, Types.VARCHAR, Types.TIMESTAMP);
preparedStatementCreatorFactory.setReturnGeneratedKeys(true);

PreparedStatementCreator preparedStatementCreator =
            preparedStatementCreatorFactory
            .newPreparedStatementCreator(Arrays.asList(users.getName(), users.getCreateDateTime()));
英文:

I ran into the same problem as you. Apparently, you used the example from the book "Spring in action".

It seems that the problem in the code, namely in the "PreparedStatementCreatorFactory" class, the "returnGeneratedKeys" field is set to false.

I do not know why the example works in the book or why it is not specified that the variable needs to be changed. The value of this variable, according to Github, has not changed since 2008.

Here is a piece of class code:

public class PreparedStatementCreatorFactory {

	/** The SQL, which won&#39;t change when the parameters change. */
	private final String sql;

	/** List of SqlParameter objects (may not be {@code null}). */
	private final List&lt;SqlParameter&gt; declaredParameters;

	private int resultSetType = ResultSet.TYPE_FORWARD_ONLY;

	private boolean updatableResults = false;

	private boolean returnGeneratedKeys = false;

	@Nullable
	private String[] generatedKeysColumnNames;

The problem is solved by setting the value of the "returnGeneratedKeys" field to true. Something like this:

var preparedStatementCreatorFactory = new PreparedStatementCreatorFactory(query, Types.VARCHAR, Types.TIMESTAMP);
preparedStatementCreatorFactory.setReturnGeneratedKeys(true);

PreparedStatementCreator preparedStatementCreator =
				preparedStatementCreatorFactory
				.newPreparedStatementCreator(Arrays.asList(users.getName(), users.getCreateDateTime()));

huangapple
  • 本文由 发表于 2020年8月6日 22:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63285627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定