The difference and usage of cascade and inverse in hibernate

Source: Internet
Author: User

Inverse and Cascade are the two most difficult attributes to master in Hibernate mappings. Both play a role in the object's associated operations.
1. Clarify the role of inverse and cascade
Inverse determines whether changes to the collection in the object are reflected in the database, so inverse only works on the collection, which is only valid for One-to-many or Many-to-many (because only the two associations contain collections. One-to-one and Many-to-one only contain a reference to each other.
Cascade determines whether changes to objects are reflected in the database, so cascade works on all associated relationships (because the association relationship refers to the relationship between objects).
2.inverse attribute: Inverse describes how the relationships between objects are maintained.
Inverse only exists in the elements of the collection tag. The collection elements provided by Hibernate include <set/> <map/> <list/> <array/> <bag/>
The purpose of the inverse property is to reflect changes to the collection object to the database.
The default value of the inverse property is false, which means that modifications to the collection object are reflected in the database, and that the Inverse=false is the active party responsible for maintaining the associated relationship.
Inverse= "true" means that modifications to the collection object are not reflected in the database.
To maintain the relationships of two entity classes (tables), some of the attributes added, the attribute may be in a two entity class (table) or in a separate table, which depends on the direct correspondence between the two parties: the maintenance here refers to the corresponding updates to the associated relationship at the same time when the master control is added to the check operation.
One-to-many: The attribute is on one side of the multiple. Should be on one side of the set inverse=true, multiple parties set Inverse=false (multiple parties can also not set the inverse property because the default value is False), which means that the association is maintained by more than one party. If you want to maintain a relationship, you will be in the insert or delete "one" side to update the "many" side of each of the object with this "one" relationship. And if the "more" aspect of maintaining the relationship there will be no update operation, because the relationship is in the various objects, directly to insert or delete multiple objects on the line. Obviously, this will reduce the number of operations and improve efficiency.
Note:
In a one-way One-to-many association relationship, inverse= "True" cannot be set because the control's mapping file does not have information from the main control party.
Many-to-many: Attributes are in a stand-alone table. The default value for the inverse property is false. In a Many-to-many association relationship, the inverse of the relationship cannot all be set to false, that is, the default is incorrect, and if all set to False, the insert operation causes a two-second relationship to be inserted in the relational table. Nor can all be set to true, and if all is set to true, no action will trigger an operation on the relational table. So set the inverse=true on either side and the other side inverse=false.
One-to-one: In fact is a pair of many special cases, inverse settings are the same, mainly to see the nature of the relationship between the party, the side of the Inverse=false.
Multiple to one: that is, a pair of many in turn, no difference.
2.cascade Properties
The function of the Cascade property is to describe the cascading properties of the associated object when it is manipulated. Therefore, only the elements involved in the relationship have Cascade attributes.
Tags with cascade properties include <many-to-one/> <one-to-one/> <any/> <set/><bag/> <idbag/> ist/> <array/>
Note: The <ont-to-many/> and <many-to-many/> are used inside the collection tag, so the Cascade attribute is not required.
Cascade operation: Refers to the same action to be performed on the associated party when the master performs an operation.
The difference between 3.inverse and Cascade
The range of functions varies:
The inverse is set in the collection element.
Cascade is valid for all elements involved in the association.
<many-to-one/><ont-to-many/> no inverse properties, but cascade properties
Different policies are being executed
Inverse will first judge the changes in the collection and then perform the appropriate processing for the change.
Cascade is a direct response to each element of the collection
The timing of the execution is different
Inverse is to decide whether to execute the SQL statement before executing the SQL statement
Cascade is used to determine whether cascading operations are performed when the main control occurs
Different targets for execution
Inverse is different for <ont-to-many> and <many-to-many> processing.
For <ont-to-many>,inverse, the modified operation of the associated table is handled.
For <many-to-many>,inverse, the Intermediate association table is being processed.
Cascade does not differentiate between the two relationships, and the actions are made against the objects being associated.
Summarize:
<one-to-many>, it is recommended that inverse= "true" be maintained by the "many" side for the association relationship
<many-to-many>, set only one of the inverse= "false", or both of them are not set
Cascade is not normally used. Especially the deletion, must be cautious.
Operational recommendations
The general Many-to-one and Many-to-many are not set cascade, which depends on the needs of the business logic, the one-to-one and One-to-many settings cascade.
Many-to-many in an association relationship, one end sets inverse= "false" and the other end is set to Inverse= "true". In One-to-many Association, set inverse= "true" to maintain the relational table by multiport

<?xml version= "1.0" encoding= "GBK"?> <!--Specifies the DTD information for the hibernate mapping file--> DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "> <!--hibernate the root element of the mapping file-->  


 

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.