In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use cascade and inverse in Hibernate". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use cascade and inverse in Hibernate".
1. Where on earth can I use cascade= "..."?
The cascade attribute 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 attribute 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 who is specified in the hbm file to maintain the relationship, and that 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 inserts the Student, the TeacherStudent table is not 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 an one-to-many, if you want one party to maintain a relationship, it will cause every object that is related to the "one" object that goes to update the "one" party when inserting or deleting the "one" party. 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.
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.
.
Thank you for your reading, the above is the content of "how to use cascade and inverse in Hibernate". After the study of this article, I believe you have a deeper understanding of how to use cascade and inverse in Hibernate, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 240
*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.