Cascade effect in Hibernate

Source: Internet
Author: User

Cascade effect in Hibernate

Only the "relationship token" has the cascade attribute;

One operation may trigger multiple association actions because of cascading cascade. The previous operation is called a "master operation", and the latter action is called "associated operation".

The possible values for the Cascade property are
All: Associated operations are performed in all cases, i.e. save-update and delete.
None: Associated operations are not performed in all cases. This is the default value.
Save-update: The associated operation is performed when Save/update/saveorupdate is executed.
Delete: The associated operation is performed when the delete is executed.
All-delete-orphan: When a node becomes an orphan node in the object graph, the node is deleted. For example, in a one-to-many relationship, student contains multiple book, which becomes an orphan node when a book is deleted in the object relationship.

The function of the CASCADE (Cascade) attribute:

Cascading refers to whether the associated object (the passive side) performs the same operation synchronously when the master performs the operation.
The relationship between Pojo and its Relationship property is the relationship between the "master-passive side", and if the relationship property is a set, then the passive is one of the elements in the set.
For example: School (School) has three attributes: Region (Address), Principal (Themaster) and student (Set, element is student)
When executing session.delete (school), Cascade decides whether to execute Session.delete (Address), Session.delete (Themaster),
Whether to perform session.delete (astudent) for each astudent.


The specific implementation of the "associated Operation" is based on "Master action":
"Master Action" "Associate Operation"
Session.saveorupdate--session.saveorupdate (execution saveorupdate actually executes save or update)
Session.save----> Session.saveorupdate
Session.udpate-Session.saveorupdate
Session.delete-Session.delete

The sequence of the master and associated operations is "save one first, then save the many, delete the many first, delete the one again, update the host, and then update the passive party"
For one-to-one, when its property constrained= "false" (the default), it can be considered a one-to-many relationship;
When its attribute constrained= "true", it can be regarded as many-to-one relationship;
For Many-to-many, it can be regarded as one-to-many.

For example: School (School) has three attributes: Region (Address), headmaster (Themaster, whose constrained= "false") and student (Set, element is student)
When Session.save (school) is executed,
The actual order of execution is: Session.save (Address);
Session.save (school);
Session.save (Themaster);
for (per student) {
Session.save (astudent);
}

When Session.delete (school) is executed,
The actual order of execution is: Session.delete (themaster);
for (per student) {
Session.delete (astudent);
}
Session.delete (school);
Session.delete (Address);

When Session.update (school) is executed,
The actual order of execution is: Session.update (school);
Session.saveorupdate (Address);
Session.saveorupdate (Themaster);
for (per student) {
Session.saveorupdate (astudent);
}

Note: The update operation is caused by cascading the saveorupdate operation instead of the update operation.
The difference between saveorupdate and update is that the former depends on whether the operation object is saved or not, and decides to perform an update or save

Extends: In practice, deleting a school does not delete the area, that is, the region's cascade is generally set to False
In addition, many-to-many relationships rarely set cascade=true, but instead set up inverse=false. This reflects the difference between the cascade and the inverse. See 4.3

The default value for cascade is false, so the inverse property defaults to associate updates.

Summary: Cascading (Cascade) is the operation of an object, as well as its properties (its cascade=true).

Comparison of Inverse and cascade
The two properties themselves do not affect each other, but are somewhat similar, and can trigger updates to the relational table.

Inverse is valid only for Set+one-to-many (or many-to-many), not for Many-to-one, one-to-one.
Cascade is valid for relationship tokens.

Inverse works on the collective object as a whole, cascade one element in the collection object, and if the collection is empty, Cascade does not raise the associated operation.
For example, set the collection object to null, School.setstudentset (NULL)
Inverse causes Hibernate to execute: udpate STUDENT set school_id=null where school_id=?
Cascade does not perform an association update to the student table because there are no elements in the collection.

More than a new school, Session.save (school)
Inverse causes Hibernate to execute:
For (each student of school) {
Udpate STUDENT set school_id=? where student_id=? Change the student's school_id to the new school ID
}
Cascade causes Hibernate to execute:
For (every student for school) {
Session.save (astudent); Perform a save operation on a student
}

Extends: If you change some of the elements in the collection (such as adding a new element),
Inverse:hibernate first determine which elements have changed and execute the corresponding SQL for the changed elements.
Cascade: It always performs an association operation on each element in the collection.
(in the associated operation, hibernate will determine whether the object of the operation has changed)

The two different timing of the work:
Cascade: Cascade occurs when operating on the main control side.
Inverse: When flush (commit automatically executes flush), determines whether each set has a change for all set,hibernate in the session,
Execution of the corresponding SQL for the changed set, before execution, there will be a judgment: if (inverse = = true) return;

Can see cascade first, inverse in the rear.

Inverse has a different effect on set + One-to-many and set + Many-to-many. Hibernate generates a different SQL.
The UPDATE statement is executed against the one-to-many,hibernate on the many side of the database table.
For Many-to-many, Hibernate executes the Insert/update/delte statement on the relational table, noting that it is not a database table for the many side but a relational table.

Cascase is consistent with set, whether One-to-many or Many-to-many. Simply passes the operation to each element in the set. So it always updates many
-Side database tables.

Recommendation: Set Inverse=false only for set + Many-to-many, other tags are set to inverse=true regardless of inverse property.

For Cascade, the one-to-one of many-to-one,many-to-many,constrained=true is not set to cascade Delete.

Cascade effect in Hibernate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.