java
67 lines · 9 steps
Soft deletes with Hibernate in Spring
Two JPA entities intercept deletes to flag rows instead of removing them, and hide the flagged rows from every query.
Explained by
highlit
1@Entity
2@Table(name = "orders")
3@SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?")
4@Where(clause = "deleted = false")
5@Getter
6@Setter
7public class Order {
8
9 @Id
10 @GeneratedValue(strategy = GenerationType.IDENTITY)
11 private Long id;
12
13 @Column(nullable = false)
14 private String reference;
15
16 @Enumerated(EnumType.STRING)
17 @Column(nullable = false)
18 private OrderStatus status;
19
20 @Column(nullable = false)
21 private boolean deleted = false;
22
23 @Column(name = "deleted_at")
24 private Instant deletedAt;
25
26 @OneToMany(
27 mappedBy = "order",
28 cascade = CascadeType.ALL,
29 orphanRemoval = true,
30 fetch = FetchType.LAZY
31 )
32 private List<OrderLine> lines = new ArrayList<>();
33
34 public void addLine(OrderLine line) {
35 line.setOrder(this);
36 this.lines.add(line);
37 }
38}
39
40@Entity
41@Table(name = "order_lines")
42@SQLDelete(sql = "UPDATE order_lines SET deleted = true, deleted_at = now() WHERE id = ?")
43@Where(clause = "deleted = false")
44@Getter
45@Setter
46public class OrderLine {
47
48 @Id
49 @GeneratedValue(strategy = GenerationType.IDENTITY)
50 private Long id;
51
52 @ManyToOne(fetch = FetchType.LAZY, optional = false)
53 @JoinColumn(name = "order_id", nullable = false)
54 private Order order;
55
56 @Column(nullable = false)
57 private String sku;
58
59 @Column(nullable = false)
60 private int quantity;
61
62 @Column(nullable = false)
63 private boolean deleted = false;
64
65 @Column(name = "deleted_at")
66 private Instant deletedAt;
67}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1@SQLDelete and @Where together turn physical deletes into reversible flag updates that stay invisible to queries.
- 2Owning both sides of a bidirectional relationship in a helper method keeps parent and child references consistent.
- 3Soft-delete flags must live on every entity in a cascade so children aren't hard-deleted out from under a soft-deleted parent.
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/soft-deletes-with-hibernate-in-spring-explained-java-4e19/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.