Hibernate is the correct way to update a collection that maps to the cascading type all-delete-orphan

What would be the correct and easy way to update a collection if it is mapped to all-delete-orphan by its parent?
When updating, I do not want to just add / remove elements from the collection, but also update the values โ€‹โ€‹of the properties of the element (of course, for those elements that were previously in the collection).

In the script there is an object Parentin which there are collections of objects Child, and there is one form with which users can edit the Children collection - add / remove children, as well as edit the properties of children (in the same form).

Basically, I want something like this:

Parent parent = session.get(Parent.class, parentUI.getId());
parent.setChildren(parentUI.getChildren());  // parentUI is a DTO
session.saveOrUpdate(parent);

This does not work, and I understand the reasons for this, but it seems to me that this should be a very common situation when using sleep mode and developing user interface applications, so I'm looking for a solution (from the book).

I am using hibernate 3.6.10 and XML based configuration.
Here's the matching mapping (I use ArrayList to store a collection of children):

<list name="children" cascade="all, delete-orphan">
    <key column="parent_id" not-null="true"/>
    <list-index column="ordinal" />
    <one-to-many class="Child" />
</list>

If that matters, the child objects also have associated collections in the same way, but I do not find this relevant, as this is a problem equal to that described.

Btw. I lost the whole day on this and, of course, checked dozens of very similar questions, but did not find a reasonable solution or template to solve this. Perhaps I missed something?

+4
1

, , , parent.updateChildren(parentUI.getChildren()), . , .

( ) ( , , .. Spring MVC) .
, , , , Spring (web) , (parentUI), , Spring ( - , ).

():

@ModelAttribute("parentUI")
public ParentUI initParentUI(HttpServletRequest p_request) {
    ParentUI parentUI = new ParentUI();
    String parentId = p_request.getParameter("id");
    if (parentId != null) { // indicates a form submit
        Parent parent = session.get(Parent.class, parentUI.getId());
        parentUI.setChildren(parent.getChildren());
    }
    return parentUI;
}

, Spring , , Hibernate.

-, , , ( ) , , .

: . .

+1

All Articles