Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to Update a Table using Sping Repository Modifying Query

How to Update a Table using Sping Repository Modifying Query

Posted on February 27, 2025 by Toma Velev

You can use Spring Data JPA’s @ Modifying and @ Query annotations to achieve Update a field of a Table using Sping Repository Modifying Query.

Spring Repository

Here is an example:

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
public interface UserRequestRepository extends CrudRepository<UserRequest, Long> {

    @Modifying
    @Query("UPDATE UserRequest u SET u.fieldCount = 0")
    void updateFieldCountToZero();
}

Note that the entity name is UserRequest and the field name is fieldCount.

Spring Service

Also, remember to add @Transactional annotation on your service method where you’re calling the repository’s update method, to ensure that the changes are committed to the database.

@Service
public class MyService {

    @Autowired
    private UserRequestRepository userRequestRepository;

    public void updateFieldCountToZero() {
        userRequestRepository.updateFieldCountToZero();
    }
}
@Service
@Transactional
public class MyService {

    @Autowired
    private UserRequestRepository userRequestRepository;

    public void updateFieldCountToZero() {
        userRequestRepository.updateFieldCountToZero();
    }
}

After all the above, you could inject the service to a @RestController or a Web @Controller. I personally use it in a Vaadin Web App. You could see some of the ready ones here: https://programtom.com/dev/product-category/technologies/spring-boot-framework/?orderby=date-desc

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)