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.
Note:Which of the preceding sections refers to the operation on which end, such as delete. If the operation is located in the <set> attribute of one end, when one is deleted, automatically delete all subrecords;
If the data is located in the <relative-to-one> label at the consumer end, the data at the consumer end will be deleted, if the foreign key still references one, an error "subrecord exists" is reported. If the cascade = "delete" attribute is set at the end of one, A very dangerous situation occurs: deleting a record at one end of the primary node will attempt to cascade the deletion of the corresponding one-End record, because one also sets cascading deletion of the primary node, therefore, all other regions associated with one will be deleted.
Therefore, you must be cautious when setting the cascade = "delete" attribute at the terminal.
Therefore, cascade is generally used in <one-to-one> and <one-to-one>
Cascade deletion is set in one-to-cascade, for example:
<Set <br/> name = "entryvalues" <br/> lazy = "false" <br/> inverse = "true" <br/> order-by = "VALUEID" <br/> cascade = "all-delete-orphan" <br/> <key> <br/> <column name = "CONTEXTENTRYID"/> <br /> </key> <br/> <one-to-operate <br/> class = "Entryvalue" <br/> </set> <br/>
If you use the SchemaExport of Hiberante to export the table to the database, the cascade attribute of the foreign key is not set in the database. Check the ENTRYVALUE table. The on delete attribute of the foreign key CONTEXTENTRYID is no action.
However, when you use Hiberante to manage a transaction, it will maintain this cascade relationship, for example:
Public void testCascadeDelete () {<br/> Session s = HibernateUtil. getSession (); <br/> Transaction tx; <br/> try {<br/> tx = s. beginTransaction (); <br/> Contextentry ce = (Contextentry) s. load (Contextentry. class, new Long (1); </p> <p> s. delete (ce); <br/> tx. commit (); </p> <p>} catch (HibernateException e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/>}< br/>}
The Entryvalue that references this Contextentry will be deleted in a correct cascade.
If common JDBC operations are used, for example:
Public void testCascadeDeleteSQL () {<br/> Session s = HibernateUtil. getSession (); <br/> Transaction tx; <br/> String SQL = "delete contextentry where id = 4 "; <br/> try {<br/> tx = s. beginTransaction (); <br/> Connection con = s. connection (); <br/> Statement st = con. createStatement (); <br/> st.exe cute (SQL); <br/> tx. commit (); <br/>} catch (HibernateException e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/>} catch (SQLException e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/>}< br/>}
The error "subrecord exists" will be reported, and the Transaction here is actually invalid, because the JDBC Connection and Statement are used, which is out of Hibernate management. if you manually set the on delete attribute of the ENTRYVALUE table to CASCADE, the preceding operation is correct-cascade delete sub-records
All-delete-orphan 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. Of course, "all child objects no longer associated with parent objects" must occur in this transaction.
Java statements for removing parent-child relationships are as follows:
Public void testCascadeDelete () {<br/> Session s = HibernateUtil. getSession (); <br/> Transaction tx; <br/> try {<br/> tx = s. beginTransaction (); <br/> Contextentry ce = (Contextentry) s. load (Contextentry. class, new Long (5); </p> <p> Entryvalue ev = (Entryvalue) s. load (Entryvalue. class, new Long (10); <br/> ev. setContextentry (null); </p> <p> s. delete (ce); <br/> tx. commit (); </p> <p>} catch (HibernateException e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/>}< br/>}
If the default value of the cascade attribute is null, the following SQL statement is executed when the parent-child relationship is removed:
Update ENTRYVALUE set CONTEXTENTRYID = null where ID = 10
When the corresponding foreign key is set to null and all-delete-orphan is used, the orphan sub-record is deleted when the related transaction is executed.