Hibernate deletes the data first, and then solves the exception when executing the insert operation. In the middle, methods such as flush () and clear () cannot be called.
A project contains an ordinary business: specify a user group for a user. A user can be assigned to multiple user groups. The backend manager can assign user groups to the user and update the user group. During the update process, all user groups are displayed in the form of checkbod. During page initialization, the checkbox of the user group that has been associated with the user is checked, then, the administrator can re-select and re-specify the user group for the user.
Well, the problem arises: When you re-specify the user group, you must first delete all records of the user ID in the original relational table (the table stores the user ID and user ID) in the database, then, save the new user group specified by the Administrator to the relational table. Therefore, at least two SQL files -- delete () and save () are required (). We are worried that the system will inevitably fail to insert data due to some special accidental reasons. In this case, we need to ensure that when this happens, delete () statement execution is invalid. After Delete (), flushu (), clear (), and commit () cannot be called () (these methods will force the original deletion of backup data in the memory to further act on the persistent layer ). However, there will be a new problem: if the data in the persistence layer is not deleted, new data cannot be inserted, otherwise, an exception such as "an object identical to this object" will be thrown. This is really a problem, but after thinking for a long time, I still want to come up with a clever solution, that is: first, use a hashmap to save the ID of each deleted relational object and the user group in the object (because the relationship between the user table and the user group table is one-to-many ), then, when inserting data, first determine whether the user group selected by the Administrator also exists in the map (determined by the user group ID). If yes, that is, the user group is originally allocated to the user. When the user starts to delete the user group, the relational object is deleted in the memory, however, the real persistence layer has not been deleted (because it does not call methods such as flush () to force execution). In this case, the relational object is retrieved from the map and the savaorupdate () method is executed directly; if the object does not exist, a new relational object is created, the user ID and user ID attributes of the object are set, and the Save () method is executed. OK, a problem is solved in this way.
The following is the business part of the project.CodeTo explain the preceding statement as follows:
// Save the relationships between users and their associated user groups (delete all records first, and then save them in a distributed manner)
// The hash table key is the user group ID, and the value is the user group associated with the user group (usrgroup)
Hashmap <long, usrgroup> map = new hashmap <long, usrgroup> ();
// Delete all user group records associated with the user, and save the user group ID and relational object to the hash table.
// This is because the flush () method is not called when the delete () method is executed, so records are not actually deleted in the database;
// Therefore, an exception such as "object already exists" will be reported when data is inserted.
List <usrgroup> List = usrgroupdao. findbyusrid (hibernatesessionfactory. getsession (), usrid );
For (INT I = 0; I <list. Size (); I ++ ){
Usrgroupdao. Delete (hibernatesessionfactory. getsession (), list. Get (I ));
Map. Put (list. Get (I). getusrgroupid (), list. Get (I ));
}
// Save the forum link associated with the topic.
String [] selectedlinklist = selectedlinkstr. Split (","); // The selectedlinkstr here is the IDs of multiple user groups specified by the Administrator. IDs are separated by commas.
For (INT I = 0; I <selectedlinklist. length; I ++ ){
Boolean bool = false;
Long groupid = long. valueof (selectedlinklist [I]);
// If the link ID associated with the topic is the same as the link ID in the deleted link object, run the saveorupdate () method.
// Only this method can be executed here. Exceptions will be reported for other methods.
If (Map. containskey (groupid )){
Usrgroupdao. Save (hibernatesessionfactory. getsession (), map. Get (groupid ));
Bool = true;
}
// If the user is a new forum link that is not associated with the current topic, a new link object is created and maintained.
If (! Bool ){
Try {
Usrgroup = new usrgroup ();
Usrgroup. setusrid (topicid );
Usrgroup. setusrgroupid (groupid );
Usrgroupdao. Save (hibernatesessionfactory. getsession (), usrgroup );
} Catch (exception e ){
E. printstacktrace ();
}
}
}