| 
20 | 20 |  * @OnDelete(action = CASCADE)  | 
21 | 21 |  * Parent parent;  | 
22 | 22 |  * </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.  | 
26 | 25 |  * <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.  | 
28 | 30 |  * <pre>  | 
29 |  | - * @ManyToOne(cascade = REMOVE)  | 
 | 31 | + * @Entity  | 
 | 32 | + * @Inheritance(strategy = JOINED)  | 
 | 33 | + * class Publication {  | 
 | 34 | + *     @Id  | 
 | 35 | + *     long id;  | 
 | 36 | + *     ...  | 
 | 37 | + *     @ElementCollection  | 
 | 38 | + *     @OnDelete(action = CASCADE)  | 
 | 39 | + *     String<String> keywords;  | 
 | 40 | + * }  | 
 | 41 | + *  | 
 | 42 | + * @Entity  | 
30 | 43 |  * @OnDelete(action = CASCADE)  | 
31 |  | - * Parent parent;  | 
 | 44 | + * class Book extends Publication {  | 
 | 45 | + *     @Column(unique = true);  | 
 | 46 | + *     String isbn;  | 
 | 47 | + *     ...  | 
 | 48 | + *     @ManyToMany  | 
 | 49 | + *     @OnDelete(action = CASCADE)  | 
 | 50 | + *     Set<Author> 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 | + * @OneToMany(mappedBy = Child_.parent, cascade = {PERSIST, REMOVE})  | 
 | 66 | + * @OnDelete(action = CASCADE)  | 
 | 67 | + * Set<Child> children = new HashSet<>();  | 
32 | 68 |  * </pre>  | 
 | 69 | + * This mapping looks redundant, but it's not.  | 
33 | 70 |  * <ul>  | 
34 | 71 |  * <li>If {@code @OnDelete(action = CASCADE)} is used in conjunction  | 
35 | 72 |  *     with {@code cascade=REMOVE}, then associated entities are fetched  | 
 | 
41 | 78 |  *     deleted in the persistence context, and are not automatically  | 
42 | 79 |  *     evicted from the second-level cache.  | 
43 | 80 |  * </ul>  | 
44 |  | - * <p>  | 
45 |  | - * Like database triggers, {@code on delete} actions can cause state  | 
46 |  | - * held in memory to lose synchronization with the database.  | 
47 | 81 |  *  | 
48 | 82 |  * @author Emmanuel Bernard  | 
49 | 83 |  */  | 
 | 
0 commit comments