The role of cascade and inverse in hibernate

Source: Internet
Author: User

Inverse and Cascade are the two most difficult properties to master in hibernate mapping. 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, that is, only for One-to-many or many-to-many (because only these 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 for all associated relationships (because association refers to the association between objects).
2.inverse Properties: Inverse describes how an association between objects is maintained.
The inverse exists only in the elements of the collection tag. Hibernate provides collection elements including <set/> <map/> <list/> <array/> <bag/>
the effect of the inverse property is whether modifications to the collection object are reflected in the database.
the default value of the inverse property is False, which indicates that modifications to the collection object are reflected in the database, and that the Inverse=false is the active party responsible for maintaining the association relationship.
inverse= "True" indicates that modifications to the collection object are not reflected in the database.
In order to maintain the relationship of two entity classes (tables), and add some properties, this property may be in two entity classes (tables) or in a separate table, this depends on the direct correspondence between the two sides: the maintenance of this refers to when the main control in the operation of the increase and deletion, the correlation relationship will be updated at the same time.
One -to-many: the property is on more than one side. Should be set on one side of the inverse=true, many of the parties set Inverse=false (more than one side can also not set the inverse property, because the default value is False), which indicates that the association is maintained by many Parties. If one side maintains the relationship, it causes the object to update the "many" side of each object that is associated with the "one" when inserting or deleting the "a" side. And if the "many" aspects of the maintenance relationship will not have an update operation, because the relationship is in the object of the multi-party, directly into the insertion or deletion of multiple objects on the line. Obviously doing so will reduce the number of operations and improve efficiency.
Note:
in a one-way One-to-many Association relationship, you cannot set inverse= "true" because there is no information for the master in the mapped file of the accused party.
Many-to-many: properties are in separate tables. The default value of the inverse property is false. In a many-to-many association relationship, both ends of the relationship cannot be set to false, that is, the default is not inverse, and if it is set to false, the insert operation causes two relationships to be inserted in the relational table. Neither can be set to true, and if it is set to true, no action is triggered on the relational table. So set the inverse=true on either side and the other side inverse=false.
one: In fact, a pair of more than a special case, inverse's settings are the same, mainly to see the relationship between the attributes of the party, the side of the Inverse=false.
many to one: that is, a pair of many in turn, no difference.
2.cascade Properties
The purpose of the Cascade property is to describe the cascading nature of the associated object as it operates. Therefore, only the elements that relate to the relationship have the Cascade attribute.
tags with cascade attributes include <many-to-one/> <one-to-one/> <any/> <set/><bag/> <idbag/> & Lt;list/> <array/>
Note: <ont-to-many/> and <many-to-many/> are used inside the collection tag, so the Cascade attribute is not required.
cascade operation: refers to whether the same action is performed on the associated party when the main control performs an operation.
the difference between 3.inverse and Cascade
the range of effects is different:
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 property, but Cascade property
different policies are executed
inverse will first determine the change of the set and then perform the corresponding processing for the change.
Cascade is to perform the appropriate processing for each element in the collection directly
The timing of execution is different
inverse is to determine whether to execute the SQL statement before executing it
Cascade is used to determine if a cascade operation is required when the main control is in action
different goals for execution
inverse is not the same for <ont-to-many> and <many-to-many> processing.
for <ont-to-many>,inverse, the modified operation on the associated table is handled.
for <many-to-many>,inverse, the Intermediate association table is processed.
Cascade does not differentiate between the two relationships, and the actions are made against the associated object.
Summary:
<one-to-many>, it is recommended to inverse= "true", the "many" side to maintain the association relationship
<many-to-many>, set only one of the inverse= "false", or neither side
Cascade, usually not used. Especially delete, must be cautious.
Operational Recommendations
In General, Many-to-one and Many-to-many do not cascade, which depends on the needs of the business logic, and the one-to-one and One-to-many are set to cascade.
Many-to-many Association, one end is set inverse= "false" and the other end is set to Inverse= "true". In the One-to-many Association, set inverse= "true" to maintain the relational table by a multi- port

From: Http://zhidao.baidu.com/link?url=iAnAAV6yCZbYeL3PWVBuFnn55i2G1SGkkG3IKudgfzIN1ATWhPnU6xeD9jbKtsUSo2eLe0fWfsjjv6SoZG4-Mq

Effects of cascade and inverse in hibernate

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.