When there is a parent-child relationship between the two parties, you can set cascade to all-delete-orphan at set.
The so-called parent-child relationship refers to the persistence cycle of the child controlled by the parent. The child object must be associated with a parent object. If a parent object is deleted, all associated child objects should be deleted cascade; if a child object is no longer associated with a parent object, the child object should be deleted.
All-deleteorphan capabilities:
1. When a parent object is saved or updated, all associated child objects are saved or updated in cascade mode, which is equivalent to saving-update for Cascade.
2. When deleting a parent object, cascade all associated child objects to delete
3. Delete all child objects that are no longer associated with parent objects
Java statements for removing parent-child relationships are as follows:
Customer. getorders (). Remove (order );
Order. setcustomer (null );
TX. Commit ();
If the default value of the cascade attribute is null, the following SQL statement is executed when the parent-child relationship is removed:
Update order set customer_id = NULL where id = 2
If you want to delete it, set:
<Set name = "orders" cascade = "all-delete-Orphan" inverse = "true">
<Key column = "customer_id"/>
<One-to-define class = "mypack. Order"/>
Run: delete from orders where customer_id = 2 and ID = 2;
Cascade values:
Save-Update: cascade storage (if the sub-object is updated after load, it will also be updated). However, it will not be deleted in cascade mode.
Delete: cascade deletion, but does not have level-level joint warranty and update
All-delete-orphan: when the parent-child relationship is removed, sub-objects that do not belong to the parent object are automatically deleted, and cascade deletion and cascade save updates are also supported.
ALL: cascade deletion and cascade update. However, child objects are not automatically deleted when the parent-child relationship is removed.
Delete-orphan: delete all objects that are not associated with the current object.
None :...
Cascade joint storage and cascade updates are always aggregated, so there is no separate save or updata
I usually use none, save-Update, all-delete-orphan, and it is still in one-to-many and many-to-many cases.
None. When saving, updating, and deleting the current object, ignore its associated objects.
Save-Update: when saving and updating the current object, you can cascade to save and update the associated object. It is used in many-to-many scenarios and is generally not used for one-to-many scenarios, one to multiple I usually use none or all-delete-orphan
Delete: deletes the current object and cascade the associated objects. I have never used this before.
All is save-update + Delete. In addition, when evict and lock are executed on the current object, evict and lock are also executed on the associated object. I have never used it alone.
Delete-orphan: Delete the object that is not associated with the current object. I have never used it alone.
All-delete-orphan, that is, all + Delete-orphan, is often used in one-to-many scenarios.
2009-09-10
How to Implement the delete-orphan cascade policy of onetoworkflow in JPA 1.0
Article category: Java programming
In JPA 1.0, cascade types include: merge, refresh, persist, remove, all
Everyone who has used hibernate knows that the above type is less of the delete-orphan type than the cascade of hibernate. However, delete-orphan is very useful. I don't know why JPA 1.0 does not support it. I heard that JPA 2.0 will be added ...... In short, this proves that the functions of JPA 1.0 are only an incomplete subset of hibernate functions.
Since we chose to use JPA 1.0 for our project, we need to find the delete-orphan implementation method under JPA 1.0. Fortunately, we use the hibernate Implementation of JPA (I believe this is also the use of most JPA users), so we have the following solution, that is, adding the annotation of Hibernate to entity:
Java code
Import javax. Persistence. cascadetype;
Import javax. Persistence. entity;
Import javax. Persistence. onetoence;
Import javax. Persistence. Table;
Import org. hibernate. Annotations. cascade;
...
@ Entity
@ Table (name = "orders ")
Public class order implements auditable, serializable
{
...
@ Onetoetype (mappedby = "order", cascade = cascadetype. All)
@ Cascade (Org. hibernate. Annotations. cascadetype. delete_orphan)
Private list <orderitem> orderitems;
...
}
Import javax. Persistence. cascadetype;
Import javax. Persistence. entity;
Import javax. Persistence. onetoence;
Import javax. Persistence. Table;
Import org. hibernate. Annotations. cascade;
...
@ Entity
@ Table (name = "orders ")
Public class order implements auditable, serializable
{
...
@ Onetoetype (mappedby = "order", cascade = cascadetype. All)
@ Cascade (Org. hibernate. Annotations. cascadetype. delete_orphan)
Private list <orderitem> orderitems;
...
}
Note: @ cascade (Org. hibernate. Annotations. cascadetype. delete_orphan. After testing, it is confirmed that after this sentence is added, Hibernate at the bottom of JPA will complete the delete-orphan action. If this sentence does not exist, it will not work. Hibernate is really powerful enough to find that JPA's annotation is useless. If you find your own annotation, you will still do it yourself ......