Considerations for updating objects using the Hibernate framework

Source: Internet
Author: User

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:
  
 
  1. SessionFactory sessionFactory = new SessionFactory();
  2. Session session = null;
  3. try{
  4. session = sessionFactory.openSession();
  5. session.beginTranscation();
  6. session.update(entity);
  7. session.getTransaction().commit();
  8. }catch(Exception e){
  9. session.getTransaction().rollback();
  10. }finally{
  11. session.flush();
  12. session.close();
  13. }



From for notes (Wiz)

Considerations for updating objects using the Hibernate framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.