小编lob*_*obs的帖子

keyHolder.getKey()返回null

为什么“ Spring in Action 5”中的代码不起作用(keyHolder.getKey()返回null,但是实体保存在DB中)?

private long savePizzaInfo(Pizza pizza) {
    pizza.setCreatedAt(new Date());
    PreparedStatementCreator psc =
            new PreparedStatementCreatorFactory(
                    "insert into PIZZA (name, createdAt) values (?, ?)",
                    Types.VARCHAR, Types.TIMESTAMP
            ).newPreparedStatementCreator(
                    Arrays.asList(
                            pizza.getName(),
                            new Timestamp(pizza.getCreatedAt().getTime())));
    KeyHolder keyHolder = new GeneratedKeyHolder();
    template.update(psc, keyHolder);
    return keyHolder.getKey().longValue();
}
Run Code Online (Sandbox Code Playgroud)

我的数据库表:

CREATE TABLE PIZZA
(
ID bigint DEFAULT (NEXT VALUE FOR 
PUBLIC.SYSTEM_SEQUENCE_12CA966F_4FFD_469C_BA69_80BB93916EF3) AUTO_INCREMENT 
PRIMARY KEY NOT NULL,
NAME varchar(50) NOT NULL,
CREATEDAT timestamp NOT NULL
);
CREATE UNIQUE INDEX PRIMARY_KEY_4 ON PIZZA (ID);
Run Code Online (Sandbox Code Playgroud)

java spring jdbctemplate spring-boot

7
推荐指数
2
解决办法
1545
查看次数

标签 统计

java ×1

jdbctemplate ×1

spring ×1

spring-boot ×1