liveBook · Manning

GeneratedKeyHolder

As explained in Spring in Action, Sixth Edition

A KeyHolder is a Spring JDBC object that captures keys generated by a database during an insert operation.

GeneratedKeyHolder captures an ID that the database assigns during an insert. Spring JDBC fills it after JdbcOperations.update(), allowing the generated value to be copied into the saved domain object. section 3.1.4

Use it with a prepared insert in a few practical steps:

  • Configure a PreparedStatementCreatorFactory with the insert SQL and input types. section 3.1.4
  • Enable generated-key retrieval with setReturnGeneratedKeys(true). section 3.1.4
  • Create a GeneratedKeyHolder and pass it to JdbcOperations.update(). section 3.1.4
  • Read the generated key and assign it to the saved object before returning it. section 3.1.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains prepared-statement setup, key capture, retrieval, and assignment without detailing key-holder internals.
Examples
Demonstrates saving a Taco, reading its generated key, assigning it to the object, and saving ingredient references.
What you can do after
Apply it via Spring JDBC to retrieve an inserted record's generated ID and copy it into the domain object.
How it connects
Connects GeneratedKeyHolder with PreparedStatementCreator, PreparedStatementCreatorFactory, JdbcOperations, and database-generated IDs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.