Summarize:
Cascade: To limit cascading operations, there are several parameters:
All: associated operations are performed in all cases.
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.
Inverse: In a one-to-many model, it can only be set on a single side, andinverse 's role is to update the primary key of the child table data to the main table after the cascade occurs. Make sure that the foreign key of the child table is not empty.
The following shows an example of a class student (one-to-many):
class table Javabean
Public class Classes {
Private Integer c_id;
Private String C_name;
Private String C_desc;
Private Set<student> Student;
...}
Class Table mapping
..... other fields omitted, write only the associated part of the code
<set name="student" table="student" cascade="save-update" inverse="false" lazy="true">
<key>
<column name="c_id" />//Here corresponds to the child table foreign KEY constraint name
</key>
<one-to-many class="com.cky.model.Student" />
</set>
The student table Bean is as follows
Public class Student {
Private Integer s_id;
Private String S_name;
Private Integer S_age;
Private Classes CLS;
....}
Student Table Mapping
... other fields omitted, write only the associated part of the code
<many-to-one name="CLS" cascade="save-update" class= "com.cky.model.Classes" fetch= "Join">
<column name="c_id" />//own FOREIGN KEY constraint name
</many-to-one>
Classes and Student are a one-to-many relationship that verifies the inverse effect
1, when the classes descade= "Save-update" inverse= "false" or do not write ( default false )
Perform the following actions:
Student st1=New Student ("St1", 11);
Student st2=New Student ("St2", 17);
set<student> stus=New hashset<student> ();
Stus.add (ST1);
Stus.add (ST2);
Above is the creation of two student objects and added to the Stus collection
Here is the creation of the classes object and the collection of students into
Classes cls=New Classes ("Class 1", "Lal", Stus);
Ss.save (CLS);
Execution Result: Note the last two update actions
Hibernate:select Max (c_id) from CLASSES
Hibernate:select Max (s_id) from STUDENT
Hibernate:insert into CLASSES (C_name, C_desc, c_id) VALUES (?,?,?)
Hibernate:insert into STUDENT (S_name, S_age, c_id, s_id) VALUES (?,?,?,?)
Hibernate:insert into STUDENT (S_name, S_age, c_id, s_id) VALUES (?,?,?,?)
Hibernate:update STUDENT set c_id=? where s_id=?
Hibernate:update STUDENT set c_id=? where s_id=?
Second, when classes descade= "Save-update" inverse= "true" when
Execution Result: Note that there is no update.
Hibernate:select Max (c_id) from CLASSES
Hibernate:select Max (s_id) from STUDENT
Hibernate:insert into CLASSES (C_name, C_desc, c_id) VALUES (?,?,?)
Hibernate:insert into STUDENT (S_name, S_age, c_id, s_id) VALUES (?,?,?,?)
Hibernate:insert into STUDENT (S_name, S_age, c_id, s_id) VALUES (?,?,?,?)
As can be seen from the results, when the maintenance relationship is abandoned, two fewer update statements
Inverse: Whether to abandon the maintenance relationship, that is, to make extra updates to the foreign keys of the child table, to ensure that the foreign key of the sub-table is its own primary key.
When cascading occurs, there are two situations:
1, if inverse is false, that is, classes Choose to maintain the relationship. First, when cascading is added , the value of the Student object is inserted into the table, and because of the need to maintain relationships, he updates the foreign key relationships for the data again.
Update the foreign keys to their primary key, so they must be related.
2, if inverse is true, that is , classes does not maintain the relationship, the first cascade will add Student the value of the object is inserted into the table, but the foreign key relationship to the data is not updated again, and if the data does not have a foreign key, it is empty, so you can see the second execution because we did not specify when we created the Student object Classes , so the results from the added table are also NULL .
Summary: Inverse can only be set on one side, andinverse 's role is to update the primary key of the child table data to the main table when the cascade occurs. Make sure that the foreign key of the child table is not empty.
The normal situation is to set inverse to true to speed up , and the foreign key of the child table data is set when the child Table object is created.
Hibernate cascade operation decryption (inverse and cascade)