Cascade and inverse in the hibernate

Source: Internet
Author: User

Both of these properties are used in a many-to-many or many-to-many relationship. and inverse is especially used for two-way relationships that we don't need in a one-way relationship.

Cascade represents whether a cascade operation is performed, and the inverse represents whether the relationship is maintained by the self.

Cascade:

The possible values for the Cascade property are

All: Associated operations in all cases, that is, save-update and delete.
None: No associated action is performed in all cases. This is the default value.
Save-update: Associate action when performing save/update/saveorupdate.
Delete: The associated action is performed when the delete is executed.

All-delete-orphan: Deletes a node when it becomes an orphan node in the object graph. For example, in a one-to-many relationship, student contains more than one book, which becomes an orphan node when a book is deleted in an object relationship.

Inverse:

The possible value of the inverse property is true or FALSE, and the default is false:

False means that the relationship is maintained by one's own, and true represents the maintenance of the relationship by the other. In a relationship, only one party to maintain the relationship, or there will be problems (resolving will be mentioned), but also must be a party to maintain the relationship, otherwise there will be the two sides to shirk responsibility, who do not care.

One more pair of examples:

There are two categories, father and child, that are a one-to-many relationship. The following section of the HBM configuration is extracted from the Father.hbm.xml.

<set name= "Children" lazy= "true" cascade= "All" inverse= "true" > <key column= "Fatherid"/> <one-to-many class= "My.home.Child"/> </set>

We know that the value pairs of cascade and inverse can have four combinations (this assumes that the Cascade value is None or all).

The following code is available:

Java code Fatherdao Fatherdao = new Fatherdao ();    Father Father = new Father ("David");    Child child1 = new Child ("David Junior One");       Child child2 = new Child ("David Junior two");    Father.add (child1);       Father.add (CHILD2); Fatherdao.save (father);

1. If cascade= "All" and Inverse= "false":

At this point you can see the log inside:

Java code//execute Father insert Hibernate:insert into father (name) values (?)    Cascade = ' All ', so cascade operations Hibernate:insert into the child (name, Fatherid) VALUES (?,?)       Hibernate:insert into the child (name, Fatherid) VALUES (?,?) Inverse = ' false ', father to maintain the relationship (you can see that these operations are superfluous) hibernate:update child set Fatherid =?    where id=? Hibernate:update Child Set Fatherid =? where id=?

2. If cascade = "none" and inverse = "false":

Java code//execute Father insert Hibernate:insert into father (name) values (?) Inverse= ' false ', so update the relationship hibernate:update child set Fatherid =?       where id=? However, because cascade= ' none ', the child is not inserted into the database, resulting in the following exception org.springframework.dao.InvalidDataAccessApiUsageException: Object references an unsaved transient instance

3. If cascade = "All" and inverse = "true"

Java code//execute Father insert Hibernate:insert into father (name) values (?)    Cascade= ' All ', executing the insertion of the child Hibernate:insert into the child (name, Fatherid) VALUES (?,?)       Hibernate:insert into the child (name, Fatherid) VALUES (?,?) However, due to inverse= ' true ', there is no maintenance of the relationship. But because of a one-to-many relationship, the relationship itself is in the "many" side of the table. Therefore, you do not need to update the relationship.

4. If cascade = "none" and inverse = "true"

Java code//only to father insert Hibernate:insert into father (name) values (?)

As you can see, for a one-to-many relationship, the relationship should be maintained by the "many" side (specifying the inverse of "one" side to true) and the corresponding cascade operation should be specified on the "one" side.

many to many:

In a many-to-many relationship, inverse can be no different from either party.

Resolving:

Why is it not possible for both parties to maintain relationships in many-to-many, because this can lead to repeated updates of the intermediate table and a duplicate value error.

So how to maintain relationships between the two sides in a many-to-many two-way relationship: It is best to let the party that controls the relationship update the relationship, and if you want the other party to maintain the relationship, then "explicitly" update the middle table when manipulating the data on this side.

Note:

Also note that in a two-way association, the association between objects is not related to the maintenance of the relational table mentioned above. One is the object/java level, one is the Hibernate database level. If you want to update one side, also update the other party's object collection, see the following code:

This is a section of code in the Person class, where people and events are many-to-many, two-way associations, and their collections in each other's classes are participants and event. The relational table is maintained by the person, so the maintenance of the object relationship is also in the person class, not the event class.

  Java code   public   void  addtoevent (event event)  {            this . GetEvents (). Add (Event );            event.getparticipants (). Add ( this ) ;   }       public   void  removefromevent ( event event)  {            this . GetEvents (). Remove (event);            event.getparticipants (). Remove ( this );   }  

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.