Annotation Interface Delete


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

Lifecycle annotation for repository methods which perform delete operations; alternatively, annotates a repository method as a parameter-based automatic query method which deletes entities.

The Delete annotation indicates that the annotated repository method deletes the state of one or more entities from the database. It may be used in one of two ways: as a lifecycle annotation, to delete a given entity instance or instances, or as an automatic query annotation, to delete all entities satisfying parameter-based conditions.

A Delete method might accept an instance or instances of an entity class. In this case, the method must have exactly one parameter whose type is either:

  • the class of the entity to be deleted, or
  • List<E> or E[] where E is the class of the entities to be deleted.

The annotated method must be declared void.

All Jakarta Data providers are required to accept a Delete method which conforms to this signature.

For example, consider an interface representing a garage:

 @Repository
 interface Garage {
     @Delete
     void unpark(Car car);
 }
 

Deletes are performed by matching the unique identifier of the entity. If the entity is versioned, for example, with jakarta.persistence.Version, the version is also checked for consistency. Attributes other than the identifier and version do not need to match. If no entity with a matching identifier is found in the database, or if the entity with a matching identifier does not have a matching version, the annotated method must raise OptimisticLockingFailureException.

Alternatively, the Delete annotation may be used to annotate a repository method with no parameter of entity type. Then the repository method is interpreted as a parameter-based automatic query method. The entity type to be deleted is the primary entity type of the repository. The method return type must be void, int, or long. Every parameter of the annotated method must have exactly the same type and name (the parameter name in the Java source, or a name assigned by @By) as a persistent field or property of the entity class. Parameters of type Sort, Order, Limit, and PageRequest are prohibited.

For example, consider an interface representing a garage:

 @Repository
 interface Garage
         extends DataRepository<Car,String> {
     @Delete
     void unparkAll();

     @Delete
     void unpark(String registration);
 }
 

Here,unparkAll() deletes every Car, while unpark(String) deletes any Car with a matching value of its registration field.

An automatic query method annotated Delete removes every record which satisfies the parameter-based conditions from the database. If the method return type is int or long, the method must return the number of deleted records.

Annotations such as @Find, @Query, @Insert, @Update, @Delete, and @Save are mutually-exclusive. A given method of a repository interface may have at most one @Find annotation, lifecycle annotation, or query annotation.

See Also: