Specific explanations for cascade and inverse in hibernate

Source: Internet
Author: User

Learning hibernate when the concept of cascade relationship is always unclear, especially cascade, inverse silly confused. The following is a simple example of how to use the sample.

Preparatory work:
Start by creating a database with two new tables:
Classroom Table classes (field here Province)
Student Table student (field here Province)
The classroom is a one-to-many relationship with students. then create a new project and add hibernate support for both tables.

1.cascade

The cascade represents a cascade operation. When there is a cascade relationship between two entities (a class is a property in a class), when an entity is saved, updated, or deleted, the corresponding action is taken on the associated entity (database operations), such as:
Save class Cascade Save Students:

Make the following changes in the Classes.hbm.xml configuration file, for example:

Now run code such as the following:

In this way, when you save classes, you will voluntarily add two new students to the database.
Description:

  • In hibernate, saving a persisted object by means of the Session.save method is called display save.

  • In Hibernate, an object is manipulated in a cascading way. Such a way is called implicit manipulation.
  • An implicit save operation is performed on the student object because student is a transient state object. There are no corresponding records in the database, so INSERT statements should be run against student

Update class CASCADE update Students:

Description

  • When running 108 rows, a SQL statement such as the following is generated

  • When running 110 lines of code, SQL statements such as the following are generated

  • When running 114 lines of code. Generates an SQL statement such as the following

The UPDATE statement produced 3, as there were 3 students in the class, and the attributes of the 3 students changed at the same time.

  • There are no UPDATE statements issued for updating classes. Because the properties of classes have not changed.

Delete class Cascade delete students:
set the Cascade for Classes.hbm.xml to delete

At this time the classes included so the student have been deleted. Note You cannot delete an object that is referenced by a foreign key if you do not set a cascade delete.

Summary
Cascade:

  • Save-update
    when an object is session.save/update, the Cascade Operation associates the object, associates the object or runs the Save statement or runs the UPDATE statement or doesn't run
  • Delete
    when session.delete an object, cascade deletes the associated object
  • All
    the combination of save-update and delete
2.inverse

The inverse property is used to indicate whether the party participates in maintaining the relationship. Set to true when not maintained and set to false when maintained.

The relationship here refers to the association of two tables

Foreign key or relational table field.

This property is generally set to one end of a one-to-many relationship. and set to False. Because if one side is responsible for maintenance, each time the data is updated, it will look for rows in a multi-segment table that has a relationship at the end and update the Outer key field. While by multi-terminal maintenance. Because one end object is a property field of a multi-object, the data is submitted after each update. It is easier to update the field on its own initiative, if one is updated.

The difference between 3.Cascade and inverse student and classes
  • Cascade describes the relationship between objects and objects.
Cascade is not related to foreign keys in the student table. SID, Name, description and cascade are related, but CID and Cascade are not related.
  • Inverse describes the relationship between an object and a foreign key
Inverse is only related to the CID. Assume maintenance. The UPDATE statement (the SQL statement that updates the foreign key) is issued. Assuming no maintenance, then no matter what.
  • The inverse property defaults to false, which means that both sides of the relationship maintain the relationship. Not maintained when inverse= "true" is set
4. Only when there is inverse no cascade:

For classes and student that do not exist in the data: When you set the student to classes, simply save classes without directly saving student and only run the Insert Classes method for student (even if it does not exist) does not insert but only update (because in the same thing it finally leads to classes can not be saved)

When the student of the classes setting to be saved is present in the database, the Update method is run on it, and if Cascade is set for classes, insert will be set for the nonexistent student:

When you set inverse= "true" to classes. Indicates that classes does not maintain and student foreign key relationships. At this time when deleting a classes, because classes does not maintain the relationship with student, Gu will not issue an UPDATE statement, so will error. Assuming that classes does not set inverse= "true", which is inverse= "false", then classes maintains the relationship. Therefore, when the deletion of classes will issue the update student statement, it can be deleted successfully.

5. Correct settings inverse

We say that the improper establishment of inverse can lead to poor performance, in fact, it is said that inverse improper establishment, will produce redundant SQL statements and even cause the JDBC exception throw.

This is where we need to be concerned when establishing entity-class relationships.

In general, Inverse=true is recommended to use, two-way association in both sides are set inverse=false, will cause both sides to update the same relationship repeatedly.

But assuming both sides set up inverse=true words, both sides do not maintain the update of the relationship, this is not good, fortunately, a multi-terminal : One-to-many default is Inverse=false, to avoid such a mistake. but many to many there is no such default settings, so very many people often use inverse=true on both ends of the many-to-many, resulting in the connection table data is not recorded at all, because they are not responsible for maintaining the relationship between the two parties. So the best setting in a bidirectional association is that one end is inverse=true. One end is Inverse=false. General Inverse=false will be placed on one end of the many, then someone asked. There are many many-to-many on both sides. Where exactly is inverse put? The fact that hibernate builds many-to-many relationships also separates them into two one-to-many relationships, connecting a connection table in the middle.

So Universal has a one-to-many relationship. It is also possible to say that one-to-many is the basic component of many-to-many.

Specific explanations for 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.