Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch3: JdbcTacoRepository.saveTacoInfo: keyHolder.getKey() returns null #101

Open
glebocki opened this issue Feb 2, 2021 · 1 comment
Open

Comments

@glebocki
Copy link

glebocki commented Feb 2, 2021

Following an answer to question on StackOverflow.

JdbcTacoRepository is missing a crucial peace: preparedStatementCreatorFactory.setReturnGeneratedKeys(true);

This is a working method:

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


    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbc.update(psc, keyHolder);

    return keyHolder.getKey().longValue();
}
@hbliyafei
Copy link

<properties>
    <!-- H2 1.4.197 breaks auto-incrementing identity columns for some reason -->
    <h2.version>1.4.196</h2.version>
</properties>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants