Improper establishment of inverse will lead to poor performance, in fact, it is said that inverse set up improperly, 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.
Cascade has five options: All, delete, None,save-update,delete-orphan; All: Associate operations 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. Delete-orphan: When save/update/saveorupdate, equivalent to Save-update, when the deletion operation, the equivalent of delete, 1, exactly where to use the 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. is 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, it means that the object is not persistence objects to 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= "true"? " The inverse property of set determines whether changes to the set are reflected in the database. Inverse=false ———— reflection; Inverse=true ———— does not reflect "inverse property default to Falseinverse property is false by default, which means that both ends of the relationship are maintained for the relationship. This means that if there is a student, teacher and teacherstudent tables, student and teacher are 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 looking at the hbm file that specifies "who" maintains the relationship, which inserts or deletes "who", the action on the relational table is sent. 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. (1) for One-to-many (set inverse=false), changing set will allow Hibernate to execute a series of update statements. (2) for Many-to-many, changing the set and only modifying the data of the relational table 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, the purpose is one: to maintain data consistency. 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. Inverse is valid only for Set+one-to-many (or many-to-many), not for Many-to-one, one-to-one. Cascade is valid for relationship tokens. Inverse the overall function of the collection object, cascade to the collection objectOne element of the Cascade function, and if the collection is empty, then the associated operation is not raised. For example, set the collection object to null, School.setstudentset (NULL) inverse cause Hibernate to execute: udpate STUDENT set school_id=null where school_id=? Cascade does not perform an association update to the student table because there are no elements in the collection. More than a new school, Session.save (school) inverse cause Hibernate to execute: for (school each STUDENT) {udpate STUDENT set school_id=? where student_id=? Changing the student's school_id to a new school Id}cascade causes Hibernate to execute: for (each Student of school) {Session.save (a Student);//Perform a save operation on students In addition, if you change some of the elements in the collection (such as adding an element), Inverse:hibernate first determines which elements are changed and performs the corresponding sqlcascade on the changed elements: it always performs the associated operation on each element in the collection. (in the associated operation, hibernate will determine if the object of the operation has changed) two times different: Cascade: Cascade occurs when you operate on a master. Inverse: When flush (commit automatically executes flush), determines whether each set has changed for all Set,hibernate in the session, executes the corresponding SQL for the changed set, and before executing, there is a judgment: if (inverse = = true) return; you can see cascade First, inverse in the back. Inverse has a different effect on set + One-to-many and set + Many-to-many. Hibernate generates a different SQL. The UPDATE statement is executed against the one-to-many,hibernate on the many side of the database table. For Many-to-many, Hibernate executes the Insert/update/delte statement on the relational table, noting that it is not a database table for the many side but a relational table. Cascase is consistent with set, whether One-to-many or Many-to-many. Simply passes the operation to each of theElements. So it always updates the many side of the database table. 4. What is the same with cascade and inverse? The two properties themselves do not affect each other, but are somewhat similar, and can trigger updates to the relational table. 5, Recommendation: Only set + Many-to-many settings Inverse=false, other tags do not consider inverse attributes, are set to Inverse=true. For Cascade, the one-to-one of many-to-one,many-to-many,constrained=true is not set to cascade Delete.
Hibernate tag Inverse cascade