8000 mention all the ways @OnDelete(CASCADE) can be used in the javadoc · hibernate/hibernate-orm@69e3f79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69e3f79

Browse files
committed
mention all the ways @onDelete(CASCADE) can be used in the javadoc
1 parent 6a97178 commit 69e3f79

File tree

1 file changed

+43
-9
lines changed
  • hibernate-core/src/main/java/org/hibernate/annotations

1 file changed

+43
-9
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/OnDelete.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,53 @@
2020
* @OnDelete(action = CASCADE)
2121
* Parent parent;
2222
* </pre>
23-
* Note that this results in an {@code on delete cascade} clause in
24-
* the DDL definition of the foreign key. It's completely different
25-
* to {@link jakarta.persistence.CascadeType#REMOVE}.
23+
* This code results in an {@code on delete cascade} clause in the DDL
24+
* definition of the foreign key.
2625
* <p>
27-
* In fact, {@code @OnDelete} may be combined with {@code cascade=REMOVE}.
26+
* The {@code @OnDelete} annotation may be applied to any field or
27+
* property representing an association or collection, or to a subclass
28+
* in a {@linkplain jakarta.persistence.InheritanceType#JOINED joined}
29+
* inheritance hierarchy.
2830
* <pre>
29-
* &#064;ManyToOne(cascade = REMOVE)
31+
* &#064;Entity
32+
* &#064;Inheritance(strategy = JOINED)
33+
* class Publication {
34+
* &#064;Id
35+
* long id;
36+
* ...
37+
* &#064;ElementCollection
38+
* &#064;OnDelete(action = CASCADE)
39+
* String&lt;String&gt; keywords;
40+
* }
41+
*
42+
* &#064;Entity
3043
* &#064;OnDelete(action = CASCADE)
31-
* Parent parent;
44+
* class Book extends Publication {
45+
* &#064;Column(unique = true);
46+
* String isbn;
47+
* ...
48+
* &#064;ManyToMany
49+
* &#064;OnDelete(action = CASCADE)
50+
* Set&lt;Author&gt; authors;
51+
* }
52+
* </pre>
53+
* <p>
54+
* The affect of {@code @OnDelete(action = CASCADE)} is quite different
55+
* to {@link jakarta.persistence.CascadeType#REMOVE}. It's more efficient
56+
* to delete a row via {@code on delete cascade}, but there's a catch.
57+
* Like database triggers, {@code on delete} actions can cause state held
58+
* in memory to lose synchronization with the database. In particular,
59+
* when an entity instance is deleted via {@code on delete cascade}, the
60+
* instance might not be removed from the second-level cache.
61+
* <p>
62+
* To alleviate this problem, {@code @OnDelete} may be used together with
63+
* {@code cascade=REMOVE}.
64+
* <pre>
65+
* &#064;OneToMany(mappedBy = Child_.parent, cascade = {PERSIST, REMOVE})
66+
* &#064;OnDelete(action = CASCADE)
67+
* Set&lt;Child&gt; children = new HashSet<>();
3268
* </pre>
69+
* This mapping looks redundant, but it's not.
3370
* <ul>
3471
* <li>If {@code @OnDelete(action = CASCADE)} is used in conjunction
3572
* with {@code cascade=REMOVE}, then associated entities are fetched
@@ -41,9 +78,6 @@
4178
* deleted in the persistence context, and are not automatically
4279
* evicted from the second-level cache.
4380
* </ul>
44-
* <p>
45-
* Like database triggers, {@code on delete} actions can cause state
46-
* held in memory to lose synchronization with the database.
4781
*
4882
* @author Emmanuel Bernard
4983
*/

0 commit comments

Comments
 (0)
0