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 @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
java
public final class ImportOrganizer { private static final Pattern IMPORT_LINE = Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");
Sorting Java imports with a regex pass
regex
sorting
text-processing
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.