简体   繁体   中英

Hibernate: updating child object

I have such structure:

// all objects have valid mapping to database
public class Child {
   private int id;
   private String name;
}
public class Parent {
   private int id;
   private String name;
   private List<Child> chidlren;
}

and I have to update specific child B inside parent A.

There are two ways:

  1. Update child's fields inside collection and update the whole object:
    Parent temp = dao.getParent(id);
    temp.getChildren.get(0).setName('test');
    dao.updateParent(temp);

  2. Update only child object:
    Child temp = dao.getChild(id);
    temp.setName('test');
    dao.updateChild(temp);

Which one is better if I want to get more perfomance?

Thank you

On the surface, I would surmise that the second solution

2.Update only child object

would be more performant.

However, the only way you determine this quantitatively would be to turn on Hibernate's show_sql , capture the SQL for Solution 1 and Solution 2, run an Explain Plan for each of your solutions, and compare the resulting Explain Plans.

You could get differing results depending on what else has changed/not changed in the Parent object and other children in the Parent.children collection. When capturing SQL for Explain Plans, you would want to try different scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM