Inverse and Cascade property configuration 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.

The

2.cascade Property
Cascade property acts as a cascading attribute that describes the operation of an associated object. 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/> & 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: Whether the same action is performed on the associated party when the master performs an operation.
 

The difference between

3.inverse and Cascade
Acts differently:

     Inverse is set in the collection element.
   Cascade is valid for all elements involved in the association.
   <many-to-one/><ont-to-many/> does not have the inverse property, but has cascade properties
different policies performed The
   inverse will first determine the changes in the collection and then perform the appropriate processing for the change.
   Cascade performs the appropriate processing on each element of the collection directly
timing of execution is different
     Inverse is to determine whether to execute the SQL statement before executing the SQL statement
     Cascade is used to determine whether the Cascade action is to be performed when the master takes action
different targets to execute
     inverse are not the same for <ont-to-many> and <many-to-many> processing.
   for <ont-to-many>,inverse The modification of the associated table is handled.
   for <many-to-many>,inverse processing is an intermediate association table
     Cascade does not distinguish between the two relationships, The action is done on the object being associated.

Summary:
<one-to-many>, it is recommended that inverse= "true" be maintained by the "many" side for associated relationships
<many-to-many>, Only one of the inverse= "false" is set, or the
Cascade is not set by either side, and is not normally used. Especially the deletion, must be cautious.
actions recommend
   generally do not cascade to Many-to-one and Many-to-many, which depends on the needs of the business logic; Set cascading for one-to-one and one-to-many.
  many-to-many in an association relationship, one end is set inverse= "false" and the other end is set to Inverse= "true". In One-to-many association relationships, set inverse= "true" to maintain the relational table by multiple-multiport.

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.