Sensei Logo Sensei Hub

Recipe - Missing annotation in update query

Description:
Queries performing write operations should have the @Modifying annotation.
Level:
error
Language:
  • java
Tags:
  • Spring
  • framework specific
  • Spring Data
  • quality
Documentation

Queries that perform a write operation (Insert, Delete, Update) should receive the Spring Data @Modifying annotation. This annotation enables the @Query annotation to perform these modifications instead of performing queries only with returning the amount of changed records in the data store.

Before
@Query("delete User u where u.active = false")
int deleteDeactivatedUsers(); // Will produce InvalidDataAccessApiUsageException
After
@Modifying
@Query("delete User u where u.active = false")
int deleteDeactivatedUsers(); // No exception
References
Recipe
id: scw:spring:data:query-modifying
version: 10
metadata:
  name: Missing annotation in update query
  shortDescription: Queries performing write operations should have the @Modifying annotation.
  level: error
  language: java
  enabled: true
  descriptionFile: descriptions/Missing_annotation_in_update_query.html
  tags: Spring;framework specific;Spring Data;quality
search:
  method:
    annotation:
      type: org.springframework.data.jpa.repository.Query
      parameters:
      - value:
          value:
            stringified:
              anyOf:
              - contains: INSERT
                caseSensitive: false
              - contains: DELETE
                caseSensitive: false
              - contains: UPDATE
                caseSensitive: false
    without:
      annotation:
        type: org.springframework.data.jpa.repository.Modifying
availableFixes:
- name: Annotate with @Modifying
  actions:
  - addAnnotation:
      annotation: '@org.springframework.data.jpa.repository.Modifying'