Hibernate:inverse, Cascade, one-to-many, many-to-many explanations

Source: Internet
Author: User

1, exactly where to use cascade= "..."?

Cascade property is not a many-to-many relationship must be used, with it just let us in the insertion or deletion of the image is more convenient, as long as the source of cascade to insert or delete, all cascade relationship will be automatically inserted or deleted. To be able to correct cascade,unsaved-value is a very important attribute. Hibernate uses this property to determine whether an object should be save or update, and if the object's ID is unsaved-value, that means it is not persistence object, save (insert) If the ID is a non-unsaved-value, it means that the object is persistence objects (which already exist in the database), as long as the update is done. The Saveorupdate method is also used for this mechanism.

2, exactly where to use inverse= "Ture"?

The inverse property defaults to false, which means that both sides of the relationship maintain the relationship. This means that if there is a student, teacher and teacherstudent tables, student and teacher are many-to-many-to-many relationships, the relationship is represented by the Teacherstudent table. So when do you insert or delete records from the Teacherstudent table to maintain relationships? When using hibernate, we do not display the Teacherstudent table to do the operation. The operation of the teacherstudent is what hibernate did for us. Hibernate is to look at the hbm file that specifies "who" maintains the relationship, which, when inserting or deleting "who", sends the action to the relational table. The premise is that the "who" object already knows the relationship, which means that the object of the other end is set or add to the "who" object. As I said earlier, inverse defaults to false, which means that the relationship is maintained at both ends, and any operation on the table will be made. When inverse= "true" is used in the end of a relationship, such as bag or set in student, it means that the relationship is maintained by another level (Teacher). This means that when this is inserted into the student, the Teacherstudent table is not manipulated, even if student already knows the relationship. Operations on a relational table are only sent when teacher is inserted or deleted. Therefore, it is not right to use inverse= "true" at both ends of the relationship, which causes any operation to be made without the action on the relational table. When both ends are inverse= "false" or the default value is, the maintenance of the Code on the relationship display is not correct, resulting in a two-time relationship being inserted into the relational table.

It is more meaningful to inverse in a one-to-many relationship. In many-to-many, on which side inverse= "true" effect is similar (in efficiency). However, in a one-to-many, if you want to maintain a relationship, you can insert or delete "a" side to update the "many" side of each object that has a relationship with the "one". And if the "many" aspects of the maintenance relationship will not have an update operation, because the relationship is in the object of the multi-party, directly into the insertion or deletion of multiple objects on the line. Of course at this time also to traverse the "many" side of each object display the changes in the operation of the relationship reflected in the DB. In any case, it is more intuitive to maintain the "many" side of the relationship.

3. What is the difference between cascade and inverse?

As you can understand, Cascade defines a cascade relationship between objects on both ends of the relationship, and inverse defines the relationship and the cascade of objects.

Several options for Cascade saves:

All: Associated operations are performed in all cases.
None: Associated operations are not performed in all cases. This is the default value.
Save-update: The associated operation is performed when Save/update/saveorupdate is executed.
Delete: The associated operation is performed when the delete is executed.

All means Save-update + DELETE
All-delete-orphan means that when an orphan node is generated in the object graph, the node is deleted in the database
All is good to understand, for example, say All-delete-orphan:
Category is a one-to-many relationship with item, meaning that there is a set type of variable items in the Category class.
For example, the current items store two item, ITEM1,ITEM2, if the relationship is defined as All-delete-orphan
When an item is deleted from items (such as removing item1 with the Remove () method), the deleted item class instance
will become an orphan node when executing category.update (), or Session.flush ()
Hibernate synchronizes the cache and the database, deleting the corresponding records in the database item1

Hibernate:inverse, Cascade, one-to-many, many-to-many explanations

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.