Skip to content

JDBC Update

@JdbcUpdate defines a JDBC method that executes a SQL update statement. The generated method creates a PreparedStatement, binds Java method parameters, applies statement configuration, executes the update, and optionally maps generated values.

The annotation value must contain a valid SQL statement with named parameters. Parameter binding is defined in parameter binding. Generated values are mapped as defined in result mapping. Converter lookup is defined in converter lookup. Return nullness follows the Nullness Contract.

❗️Update methods

Without generated values, an @JdbcUpdate method may return:

  • void: ignores the update count
  • int: returns the value from PreparedStatement.executeUpdate()
  • boolean: returns true when the update count is greater than zero

Other return types are supported only when generated values are enabled.

Generated values are enabled with @JdbcUpdate(returnGeneratedValues = ...).

The supported generated-value modes are:

  • GENERATED_KEYS: execute the update and read PreparedStatement.getGeneratedKeys()
  • EXECUTE_QUERY: execute the statement with executeQuery()
  • DEFAULT: use the configured default generated-value mode

Generated-value return types must be supported value or row return types. They are mapped with the same result-mapping rules used by @JdbcSelect.

Generated-value return nullness must be unspecified or @NonNull. @Nullable and Optional<T> return types are invalid.

These generated-value return forms are invalid:

  • void
  • collection return types
  • JdbcBatch
❗️Batch update methods

@JdbcBatchUpdate defines a JDBC method that creates a batch object. The return type must be an interface declared inside the same JDBC service interface as the @JdbcBatchUpdate method. That interface must extend JdbcBatch. The return type nullness must be unspecified or @NonNull; @Nullable and Optional<T> are invalid.

The batch interface must declare exactly one non-static, non-default @JdbcUpdate method. That method must be annotated with @JdbcUpdate and must return void. Its parameters are bound with the normal parameter-binding rules.

Calling the batch update method binds its parameters and adds one entry to the underlying PreparedStatement batch. The generated batch automatically executes collected entries when the configured batch size is reached.

JdbcBatch.executeBatch() executes currently collected batch entries and resets the current batch count. If no entries are collected, it returns an empty result.

JdbcBatch.close() is resource cleanup. It does not execute collected batch entries. If collected entries are still pending, close() throws an exception. Call executeBatch() before closing when entries are still pending.

@JdbcBatchSize defines the automatic execution size. It may be declared as a constant on the @JdbcBatchUpdate method or as one method parameter.