java
18 lines · 5 steps
Bulk JPQL updates in a Spring Data repository
A Spring Data repository runs set-based UPDATE statements directly against the database instead of loading and saving entities one by one.
Explained by
highlit
1@Repository
2public interface SubscriptionRepository extends JpaRepository<Subscription, Long> {
3
4 @Modifying(clearAutomatically = true, flushAutomatically = true)
5 @Query("""
6 UPDATE Subscription s
7 SET s.status = :status,
8 s.deactivatedAt = :now
9 WHERE s.status = com.acme.billing.SubscriptionStatus.ACTIVE
10 AND s.currentPeriodEnd < :now
11 """)
12 int expireOverdueSubscriptions(@Param("status") SubscriptionStatus status,
13 @Param("now") Instant now);
14
15 @Modifying
16 @Query("UPDATE Subscription s SET s.reminderSent = true WHERE s.id IN :ids")
17 int markRemindersSent(@Param("ids") Collection<Long> ids);
18}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Bulk JPQL updates run set-based SQL in one round trip, bypassing per-entity loading entirely.
- 2Because bulk updates skip the persistence context, clearing and flushing keeps in-memory entities from going stale.
- 3A modifying query returns the affected row count, giving you a cheap signal of how much work happened.
Related explainers
java
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class TenantResolutionFilter extends OncePerRequestFilter {
How a tenant-resolution filter works in Spring
multi-tenancy
servlet-filter
thread-local
Intermediate
8 steps
java
package com.example.uploads.config; import jakarta.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory;
Configuring upload size limits in Spring
file-upload
exception-handling
http-errors
Intermediate
6 steps
java
public final class Levenshtein { private Levenshtein() { }
Two-row Levenshtein distance in Java
dynamic-programming
edit-distance
space-optimization
Intermediate
8 steps
java
package com.example.lb; import java.util.List; import java.util.concurrent.atomic.AtomicInteger;
A thread-safe round-robin load balancer in Java
concurrency
load-balancing
round-robin
Intermediate
9 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/bulk-jpql-updates-in-a-spring-data-repository-explained-java-7655/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.