Inverse and cascade are the two most difficult attributes in hibernate ing. Both play a role in object Association operations.
1. define the role of inverse and cascade
Inverse determines whether to reflect the changes made to the objects in the database. Therefore, inverse only applies to the set, that is, it is only valid for one-to-Minus or minus-to-minus (because only these two associations contain a set, one-to-one and others-to-one only contain a reference of the other party ).
Cascade determines whether to reflect changes to objects in the database. Therefore, cascade applies to all associations (because associations refer to associations between objects ).
2. Inverse attributes: Inverse describes how to maintain the association between objects.
Inverse only exists in the Elements marked by the set. . The collection elements provided by hibernate include <set/> <MAP/> <list/> <array/> <bag/>
The inverse attribute indicates whether to reflect changes to the set object to the database.
The default value of the inverse attribute is false, indicating that changes to the set object will be reflected in the database;If inverse is set to false, the active party is responsible for maintaining the association.
Inverse = "true" indicates that changes to the set object will not be reflected in the database.
To maintain the relationship between two object classes (tables), some attributes added may exist in two object classes (tables) or in an independent table, this depends on the direct correspondence between the two parties: the maintenance here refers to the corresponding update of the association relationship when the master database adds, deletes, modifies, and queries the data.
One to multiple:This attribute is on multiple sides. Set inverse = true on one side and inverse = false on the other side (the inverse attribute cannot be set on multiple sides, because the default value is false ), this indicates that the Association is maintained by multiple parties. If one party needs to maintain the related system, it will update every object of the "multiple" side related to this "one" object when "one" side is inserted or deleted. If you want to maintain a "multi" relationship, there will be no update operation, because the relationship is in the Multi-Party object, just insert or delete the Multi-Party object. Obviously, this will reduce a lot of operations and improve efficiency.
Note:
Inverse = "true" cannot be set in one-way one-to-one relationship, because the referenced ing file does not contain information from the master.
Many to many:The attribute is in an independent table. The default value of the inverse attribute is false. In many-to-many associations, the inverse at both ends of the relationship cannot be set to false, that is, the default situation is incorrect. If both are set to false, during the insert operation, the relationship is inserted twice in the relational table. Neither can be set to true. If both are set to true, any operation on the relational table is not triggered. Therefore, set inverse = true on either side and inverse = false on the other side.
One to one:In fact, it is a special case of one-to-many, and the inverse settings are the same. It mainly depends on which side of the association is located, and the inverse of this side is false.
Many-to-one:That is, there is no difference between one and multiple.
2. Cascade attributes
The role of the cascade attribute is to describe the cascading features of associated objects during operations. Therefore, only the elements involved in the relationship have the cascade attribute.
Tags with cascade attributes include <allow-to-one/> <one-to-one/> <Any/> <set/> <bag/> <idbag/> <LIST/> <array/>
Note: <ont-to-sign/> and <ont-to-sign/> are used inside the set tag, so the cascade attribute is not required.
Cascade operation: Indicates whether to perform the same operation on the associated party when the primary prosecution executes an operation.
3. differences between inverse and cascade
different scopes:
inverse is set in the collection element.
cascade is valid for all related elements.
the inverse attribute is not available, but the cascade attribute is available.
different execution policies
inverse first checks the changes in the set and then processes the changes accordingly.
cascade directly processes each element in the set
the execution time is different
inverse determines whether to execute the SQL statement before executing the SQL statement
cascade occurs in the primary control. used to determine whether cascade operations are required
different execution targets
inverse has different processing methods for and .
for , inverse modifies the joined table.
inverse processes intermediate join tables.
cascade does not distinguish the two relations, all operations are performed on associated objects.
conclusion:
, inverse = "true" is recommended ", in
, only one of the two parties is set to inverse = "false ", or both parties do not set
cascade, which is usually not used. In particular, you must be careful when deleting a file.
operation suggestion
generally, cascade is not set for sequence-to-one and sequence-to-sequence, this depends on the needs of the business logic; Set cascade for one-to-one and one-to-one.
in a sequence-to-sequence Association, inverse = "false" is set at one end and inverse = "true" is set at the other end ". In the one-to-one relationship, set inverse = "true" to maintain the relationship table on multiple ends.