Hibernate when updating objects, if some of the object's properties are not set, then at the time of the update, the default is empty. Especially when updating a Form object. Example: Department Department class This department class has the following properties: Id:long name:string (note: Department name) description:string (Note: Department description) Parent:department (Note: parent department) c Hildren:set<department> (note: subordinate department)
Modify the page form: Except for the Children property, the other properties have a corresponding input box (where the ID is a hidden field).
The page form object that the page submits to is an entity that contains only the ID, name, description, parent, and if it is updated directly with the object, the Children property before the object is modified is empty. In other words, if the "Development Department" object, through Session.update (department), "Development department" under the object of "Research and Development Group", "programming group" and other subordinate departments have become the department without the parent department. If you need to change only the Name property and the description, parent property of the Department of the Development Department, and not change the original "Children" attribute, then we will use the following method to update instead of directly. First, find the persisted object in the database by using the ID of the "development department" passed by the page table only son, and then assign the value by the set () method to the Name,description,parent property of the persisted object; Using the Session.update () method to update the persisted object, only the Name,description,parent property of the persisted object is updated. Without changing the original children attribute.
The reference code for the session update object:
SessionFactory sessionFactory = new SessionFactory();
Session session = null;
try{
session = sessionFactory.openSession();
session.beginTranscation();
session.update(entity);
session.getTransaction().commit();
}catch(Exception e){
session.getTransaction().rollback();
}finally{
session.flush();
session.close();
}
From for notes (Wiz)
Considerations for updating objects using the Hibernate framework