Annotation Interface Update


@Documented @Retention(RUNTIME) @Target(METHOD) public @interface Update

The Update annotation indicates that the annotated repository method requests that one or more entities be updated if found in the database. This method must have a single parameter whose type must be one of the following:

  • The entity to be updated.
  • An Iterable of entities to be updated.
  • An array of entities to be updated.

The return type of the annotated method must be void, boolean, a numeric primitive type (such as int), a corresponding primitive wrapper type (such as Integer), or the same type as the parameter.

A boolean return type indicates whether a matching entity was found in the database to update. A numeric return type indicates how many matching entities were found in the database to update. An entity return type indicates the updated entity if found in the database. If the entity is not found in the database or has a non-matching version, then null is returned. An Iterable or array return type includes all matching entities that are found in the database, skipping over entities that are not present in the database or have a non-matching version. For example, if the method is annotated with @Update and takes a parameter of type Car car, the return type can be Car. Similarly, if the parameter is an Iterable<Car> or an array of Car, the return type can be Iterable<Car>. Entities that are returned by the annotated method must include all values that were written to the database, including all automatically generated values, updated versions and incremented values that changed due to the update. The order of entities within an Iterable or array return value must correspond to the position of entities in the parameter based on the unique identifier of the entity, leaving out those that did not match the unique identifier and version that is in the database.

Updating an entity involves modifying its existing data in the database. The method will search for the entity in the database using its ID (and version, if versioned) and then update the corresponding record with the new data. After invoking this method, do not continue to use the entity value that is supplied as a parameter, as it may not accurately reflect the changes made during the update process.

If the entity does not exist in the database or it is versioned and its version differs from the version in the database, no update is made and no error is raised.

In databases that use an append model to write data or follow the BASE model, this method behaves the same as the @Insert method.

For example, consider an interface representing a garage:

 @Repository
 interface Garage {
     @Update
     Car update(Car car);
 }
 

If this annotation is combined with other operation annotations (e.g., @Insert, @Delete, @Save), it will throw an UnsupportedOperationException because only one operation type can be specified. A Jakarta Data provider implementation must detect (and report) this error at compile time or at runtime.