In-Hibernate relational operations (inverse) and cascading operations (CASCADE)

Source: Internet
Author: User

Explain inverse (relational operations) and (cascade) operations with user, role, and user files as an example

Inverse evaluates to True (does not maintain relationship) or false (Maintenance relationship defaults to false) This property primarily operates on foreign keys

Cascade value Null (default), Save-update, all, delete

Users, roles are many-to-many relationships

The user's mapping file represents:

<class name= "Com.xing.elec.domain.ElecUser" table= "Elec_user" >

。。。。。

<set name= "Elecuserfiles" table= "Elec_user_file" Inverse= "true" order-by= "progresstime desc"  cascade= "delete" >
<key>
<column name= " UserID "></COLUMN>
</key>
<one-to-many class= "Com.xing.elec.domain.ElecUserFile"/>
</set>

<set name= "Elecroles" table= "Elec_user_role" inverse= "true">
<key>
<column name= "UserID" ></column>
</key>
<many-to-many class= "Com.xing.elec.domain.ElecRole" column= "Roleid" ></many-to-many>
</set>

</class>

mapping files for roles:

<class name= "Com.xing.elec.domain.ElecRole" table= "Elec_role" >

。。。。

<set name= "Elecusers" table= "Elec_user_role" >
<key>
<column name= "Roleid" ></column>
</key>
<many-to-many class= "Com.xing.elec.domain.ElecUser" column= "UserID"/>
</set>

</class>

Relational operations:

Case one:

The relationship established between user and user files (foreign key) is maintained by user files because the user and user files are set to inverse in the associated set label.

For example, when adding a user, a user has multiple user files to execute elecuserfile.setelecuser (elecuser) before inserting data into the database; the line code

The purpose of a row code is to associate a user and user file with a value for the corresponding foreign key in the user's file table when the data for the user and user files is inserted into the database .

Case TWO:

The associated set label set to Inverse= "false" (the default is False) because the user and the role (that is, the user table) establish an associated set label with the inverse setting value of true while the role user (i.e.: Role table) is established:

Remove user and role affinity (related data in the User Role table) when deleting a user

If the user is used to delete the related data in the User Role table, the operation will report an exception (an exception exists where the foreign key cannot be removed)

The right thing to do: is to get the user's set collection by using the user to obtain all the roles (set collection) and then traverse the set collection to get the user to each role through the role in the get user's sets set and then delete the user

( using roles to delete data in a user role table is actually using a role to manipulate foreign keys )

Code implementation:

Set<elecrole> Elecroles=user.getelecroles ();
if (Elecroles!=null && elecroles.size () >0) {
for (Elecrole elecrole:elecroles) {
(Delete a user by using the role to get the set collection of the encapsulated user)
Elecrole.getelecusers (). Remove (user);
}
}


Cascade Operations

Case one:

Condition: Cascade is set to delete because the user and user files are established in the associated set label

Result: User file information associated with the user is deleted when the user information is deleted (This is a cascade delete operation)

Summarize:

Inverse: Used to maintain relationships in general, we use many Parties to maintain one side (so efficient) and one side to maintain more of the party (so that the efficiency of implementation is not good)

Cascade: Mainly used for one-to-one and one-to-many is not recommended

Many-to-many reasons for using Cascade are not recommended (take user roles as an example): If you delete a user, the User role table associated with the user table and the related data in the role table are deleted and the data in the role table should not be deleted

In-Hibernate relational operations (inverse) and cascading operations (CASCADE)

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.