Detailed 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 an example to illustrate briefly.

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

Cascade represents cascading operations where there is a cascading relationship between two entities (a class is a property in another class), and if an entity is saved, updated, or deleted, the corresponding action is made on the associated entity (Database operations), for example:
Save class Cascade Save Students:

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

Now execute the following code:

This will automatically add two new students to the database when the classes is saved.
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, which is called implicit manipulation.
  • The student object is implicitly saved because student is a temporary state object that does not have a corresponding record in the database, so the INSERT statement should be executed on the student

Update class CASCADE update Students:

Description

  • When executing 108 rows, the following SQL statement is generated

  • When executing 110 lines of code, the following SQL statement is generated

  • When executing 114 lines of code, the following SQL statement is generated

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

  • There is no UPDATE statement issued that updates 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 this classes contains so the student are deleted together. Note You cannot delete an object that is referenced by a foreign key if you do not set the cascade delete.

Summary
Cascade:

  • Save-update
    when an object is session.save/update, the Cascade Operation associates the object, associates the object or executes the Save statement or executes the UPDATE statement or does nothing
  • 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 maintenance relationships, is set to true when not maintained, and is set to false when maintained. The relationship here refers to the association of two tables

Foreign key or relational table field. This property is typically set at one end of a one-to-many relationship, and is set to false because, if maintained by one end, each end of the data is updated to look for rows in a multi-segment table that has a relationship at the end and to update the Outer key field. In the case of multi-terminal maintenance, since one end object is a property field of a multi-object, it is convenient for the field to be automatically updated (if there is an update) when the data is submitted after each update.

The difference between 3.Cascade and inverse student and classes
  • Cascade describes the relationship between an object and an object
Cascade is not related to foreign keys, sid, Name, description, and cascade are related in the student table, but CID and Cascade have no relationship.
  • Inverse describes the relationship between an object and a foreign key
Inverse is only related to CID, and if maintained, the UPDATE statement (the SQL statement that updates the foreign key) is issued, regardless of if it is not maintained.
  • 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 the inverse does not have cascade:

For classes and student that do not exist in the data: When the student is set to classes, only the method of saving classes without directly saving student will only execute the Insert classes and for student (even if it does not exist) does not insert but only update (because in the same thing it eventually causes classes to be saved)

When the student of the classes setting to be saved is present in the database, the Update method is executed, and if Cascade is set for classes, the non-existent student setting is insert:

When classes is set to Inverse= "true", it 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. If classes is 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 will lead to poor performance, in fact, it is said that inverse improper establishment, will produce redundant SQL statements or even cause the JDBC exception throw. This is where we need to be concerned when building entity-class relationships. In general, Inverse=true is the recommended use, two-way association between the two sides set Inverse=false, will cause both sides to repeat the same relationship update. But if both sides set up inverse=true words, both sides do not maintain the update of the relationship, this is not good, Fortunately, one to many multi-terminal: One-to-many default is Inverse=false, to avoid the emergence of such errors. but many-to-many does not have this default setting, so many people often use inverse=true on both ends of many-to-many, which results in no record of the data in the connection table, because neither of them is responsible for maintaining the relationship. So, the best thing about bidirectional correlation is that one end is inverse=true and the other side is inverse=false. General Inverse=false will put on more than one end, then someone asked, many-to-many on both sides are more, inverse exactly where? In fact, hibernate establishes many-to-many relationships and separates them into two one-to-many relationships, connecting a connection table in the middle. So the general existence of a one-to-many relationship, it can also be said: A pair of many is the basic component of many.

Detailed 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.