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
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
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@GetMapping("/files/{id}") public ResponseEntity<StreamingResponseBody> download( @PathVariable String id, @RequestHeader(value = HttpHeaders.RANGE, required = false) String rangeHeader) throws IOException {
HTTP range requests in Spring
http-range
streaming
file-io
Advanced
10 steps
java
public List<int[]> merge(int[][] intervals) { if (intervals.length == 0) { return new ArrayList<>(); }
Merging overlapping intervals in Java
intervals
sorting
greedy
Intermediate
7 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.