In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use cascade and inverse in Hibernate". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use cascade and inverse in Hibernate".
1. Where on earth can I use cascade= "..."?
The cascade attribute in Hibernate does not have to be used in many-to-many relationships. It just makes it easier for us to insert or delete objects. As long as we insert or delete objects at the source of cascade, all cascade relationships will be automatically inserted or deleted. In order to correctly cascade,unsaved-value is a very important attribute. Hibernate uses this attribute to determine whether an object should be save or update. If the object's id is unsaved-value, it means that the object is not persistence object or save (insert); if id is non-unsaved-value, then the object is persistence object (already exists in the database), as long as update is fine. The saveOrUpdate method uses the same mechanism.
2. Where on earth do I use inverse= "ture"?
"the inverse property of set determines whether changes to set are reflected in the database. Inverse=false---- reflects; inverse=true---- does not reflect" the inverse property defaults to false.
The inverse attribute in Hibernate is false by default, which means that both sides of the relationship maintain the relationship. This means that if there is a Student, Teacher and TeacherStudent table, Student and Teacher are many-to-many relationships, which are represented by the TeacherStudent table. So when do you insert or delete records from the TeacherStudent table to maintain the relationship? When using hibernate, we will not show the operation on the TeacherStudent table. The operation on TeacherStudent is done by hibernate for us. Hibernate is to see that the "who" is specified in the hbm file to maintain the relationship, and the operation on the relational table will be issued when the "who" is inserted or deleted. The premise is that the "who" object already knows about the relationship, that is, the object on the other side of the relationship has already set or add into the "who" object. As mentioned earlier, inverse defaults to false, which means that both ends of the relationship maintain the relationship, and an operation on the table is issued for any one of these operations. When inverse= "true" is used in one end of a relationship, such as bag or set in Student, it means that the relationship is maintained by another Teacher. That is, when this is inserted into the Student, the TeacherStudent table will not be manipulated, even if the Student already knows about the relationship. Operations on relational tables are issued only when Teacher is inserted or deleted. Therefore, when it is wrong to use inverse= "true" on both sides of the relationship, it will result in any operation not issuing operations on the relational table. When both ends are inverse= "false" or default values, the maintenance of the relationship display in the code is also incorrect, resulting in the relationship being inserted twice in the relationship table.
Inverse makes more sense in an one-to-many relationship. In many-to-many, at which end the effect of inverse= "true" is similar (in terms of efficiency). But in one-to-many, if one party is required to maintain the relationship, it will cause every object related to the "one" object that goes to update the "one" when inserting or deleting the "one". If you let "multiple" aspects maintain a relationship, there will be no update operation, because the relationship is in a multi-party object, just to insert or delete a multi-party object. Of course, it is also necessary to traverse the "many" side of each object to show changes in the operation of the repair relationship reflected in the DB. In any case, it is more intuitive for "many" parties to maintain relationships.
(1) for one-to-many, changing set will cause hibernate to execute a series of update statements without delete/insert data
(2) for many-to-many, changing the set only modifies the data of the relational table and will not affect the other side of the many-to-many.
(3) although the database operations of one-to-many and many-to-many are different, they both have the same purpose: to maintain the consistency of data.
3. What's the difference between cascade and inverse?
It can be understood that cascade defines a cascade of objects to objects at both ends of a relationship, while inverse defines a cascade of relationships and objects.
Hibernate's inverse is only valid for set+one-to-many (or many-to-many), but not for many-to-one, one-to-one. Cascade is valid for relational tags.
Inverse works on the collection object as a whole, cascade works on an element in the collection object, and if the collection is empty, cascade does not trigger an associated operation.
For example, set the collection object to null, school.setStudentSet (null)
Inverse causes hibernate to execute: udpate STUDENT set SCHOOL_ID=null where SCHOOL_ID=?
Cascade does not perform associated updates to the STUDENT table because there are no elements in the collection.
Add a new school, session.save (school)
Inverse causes hibernate to execute:
For (to (every student of school) {
Udpate STUDENT set SCHOOL_ID=? Where STUDENT_ID=? / / change the student's school_id to the new school's id
}
Cascade causes hibernate to execute:
For (for every student of school) {
Session.save (aStudent); / / A pair of students perform save operations
}
Extends: if you change some elements in the collection (such as adding an element)
Inverse: hibernate first determines which elements have changed, and executes the corresponding sql on the changed elements
Cascade: it always performs an association operation on each element in the collection.
(in the associated operation, hibernate determines whether the object of the operation has changed.)
The timing of the two works is different:
Cascade: cascading occurs when operating with the master.
Inverse: when flush (commit automatically executes flush), determine whether each set has changed for all set,hibernate in session.
Execute the corresponding sql for the changed set. Before execution, there will be a judgment: if (inverse = = true) return; can see that cascade comes first and inverse comes later.
Inverse has different effects on set + one-to-many and set + many-to-many. Hibernate generates a different sql.
Execute the update statement against the database table of the one-to-many,hibernate to the Mandy side.
For many-to-many, hibernate executes the insert/update/delte statement on the relational table, not on the Mandy side's database table, but on the relational table.
Cascase is consistent with set, regardless of one-to-many or many-to-many. Simply pass the operation to each element in the set. So it always updates the database tables of the many side.
4. What are the similarities between cascade and inverse in Hibernate?
These two attributes themselves do not affect each other, but they play a similar role and can trigger updates to the relational table.
5. Suggestion: only set inverse=false to set + many-to-many, other tags do not consider the inverse attribute, all set to inverse=true. For cascade, cascading deletion is not set for the one-to-one of many-to-one,many-to-many,constrained=true.
The above is all the contents of the article "how to use cascade and inverse in Hibernate". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.