[Reprinted] the usage of cascade in databases is different in MySQL and Hibernate.

Source: Internet
Author: User

Parent-Child tables are often involved when adding, modifying, and deleting records in a database.

For example, a province table and a city table have a foreign key province_id referenced to the primary key of the province table. In this way, we can regard the province table as a parent table and the city table as a sub table. The existence of city table records depends on the records of the province table. (In the example mentioned in this article, all the code is included in the attachment, so the description here is simplified)

I. cascade in MySQL

The following operations are performed on the province table and city table directly on the MySQL console:

Add a "Guangdong" record in the province table, add a "Guangzhou" record in the city table, and reference the "Guangzhou" foreign key to the "Guangdong" primary key. The existence of "Guangzhou" depends on "Guangdong". If you delete the "Guangdong" of the province table, it will affect the "Guangzhou" of the city table ". Based on the on delete settings of the foreign key constraints of the city table, there are three situations:

1. The foreign key does not have the on delete setting: When "Guangdong" is deleted, MySQL reports an error and the deletion fails.

2. Set the foreign key to on delete cascade: When "Guangdong" is deleted, "Guangzhou" is also deleted.

3. set the foreign key to on delete set null: When "Guangdong" is deleted, the foreign key province_id of "Guangzhou" is automatically set to null, that is, "Guangzhou" is separated from its dependency on "Guangdong.

Ii. cascade in Hibernate

The following uses Hibernate to operate the province table and city table:

First, configure the relevant attributes for connecting to the MySQL database in the hibernate. cfg. xml file (modify the password for logging on to the database ). Then, add the relevant POJO class and XML ing files for the province and city tables. Create a table using an SQL statement (in test_cascade. SQL in the attachment). The city table has a foreign key province_id referenced to the primary key of the province table, and set this foreign key to on delete cascade. This foreign key constraint changes to a two-way ing relationship in Hibernate: The City class has a Province attribute of the type, which is associated with the province table, the many ing file contains the relationship between Province-to-one. The Province class has a Set <City> cities attribute, which is associated with the City table, in the ing file, the relationship is one-to-one.

In the Hibernate ing file, you can also set the cascade attribute to control the parent-child relationship. The cascade attribute is usually set in the parent table in the following situations:

1. cascade attribute not set

AddInNoCascade () is used to add records "Guangdong" and "Guangzhou" (The method is in the CityManager class, which is the same as below), and then delete () to delete "Guangdong". An exception will occur, the system will say that "Guangdong" cannot be deleted because it is associated with the city table foreign key. When using SQL to create a table, the foreign key has been set to on delete cascade. Why can't I delete "Guangdong" and cascade "Guangzhou" at the same time? You can use MyEclipse to view the city table and find two foreign keys ,:



2nd Foreign keys are generated when SQL is used for table creation, and on delete cascade is set. Hibernate automatically creates 1st Foreign keys when Hibernate is used for database operations. The On delete operation of the 1st Foreign keys is set to No action. Therefore, when "Guangdong" is deleted, the deletion fails due to the restriction of this foreign key.

2. Set the cascade attribute to delete-orphan.

In the proing file Province. hbm. in xml, Set cascade = "delete-orphan" in the Set corresponding to the one-to-one relationship. This function is the same as setting the foreign key in MySQL to on delete cascade. Delete () to delete "Guangdong". The deletion is successful. That is, when cascade is set to delete-orphan, the records of the child table will be deleted when the parent table record is deleted.

3. Set the cascade attribute to all.

Cascade attributes include delete-orphan, create, update, delete, and all. All indicates all attribute values except delete-orphan. When cascade is set to all, adding or modifying the records of the parent table will affect the related records of the child table.

In the proing file Province. hbm. xml, Set cascade = "all" in the Set corresponding to the one-to-Everything relationship ". AddInCascadeOfAll () is used to add the record "Guangdong". In this method, only save "Guangdong" is used, but not save "Shenzhen ", only the property is associated with the relationship between "Guangdong" and "Shenzhen. The results show that Shenzhen is also added to the database, which is the role of cascade = "all", so that operations on the parent table affect the sub-table.

Note:: A and delete-orphan are special attribute values and can only be applied to the cascade attribute of the one-to-one relationship. B. the cascade attribute is usually applied in the one-to-one and one-to-one relationships. It is not recommended to apply it in the inter-to-one or inter-to-minus relationships.

Iii. Summary

1. Set cascade in MySQL and set cascade in Hibernate. Cascade is set in MySQL, and does not play a role in Hibernate operations, because Hibernate automatically adds a foreign key to the sub-table.

2. cascade is used to facilitate database operations, so that operations on a table will affect the records of other tables. However, cascade functions pose security risks. Especially in Hibernate, modifying the ing reference attribute of a POJO object will affect the POJO object corresponding to this reference attribute. For example, clear the cities attribute of the Set <City> type in Guangdong (that is, call the clear () method for the Set ), in this case, the "Shenzhen" referenced in "Guangdong" is deleted. Therefore, exercise caution when using cascade functions.

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.