TreeTable & HierarchicalContainer - Storing/loading hierarchy in/from busin

hi!

i’ve got a TreeTable with HierarchicalContainer bound to it. The container holds business objects of that type:

public class ProtocolItem
{
    private UUID id;
    private UUID parentId;
    private Integer index;
    private String caption;
    private String itemType;

    public ProtocolItem()
    {
    }

...
}

I’m filling the container with a List by iterating through the List and adding each item

container.addItem and then setting the child-parent-relation

container.setParent(item, parent) After that I’m binding the container to the TreeTable and I can see the hierarchy.

I drag and drop new items to the TreeTable and that works fine, too Snippet:

if (location == VerticalDropLocation.MIDDLE) { if (container.setParent(item, parentItem) && container.hasChildren(parentItem)) { //add item at the end Integer count = container.getChildren(parentItem).size(); container.addItemAt(count); } } else if (location == VerticalDropLocation.TOP) { final Object parentId = container.getParent(parentItem); //parentItem = item on same level --> getParent = parent of both! if (container.setParent(item, parentId)) { container.moveAfterSibling(item, parentItem); container.moveAfterSibling(parentItem, item); } } else if (location == VerticalDropLocation.BOTTOM) { final Object parentId = container.getParent(parentItem); //parentItem = item on same level --> getParent = parent of both! if (container.setParent(item, parentId)) { container.moveAfterSibling(item, parentItem); } } But now I want to store the hierarchy in a database. So I want to get the values for Id, ParentId and Index out of the container for each item to store them.

I can retrieve id and parentId. But the index doesn’t work.
I tried container.indexOfId(item) - that returns an integer. But that integer
doesn’t change
after the item is moved e.g. by

container.moveAfterSibling(...) .

is there any best practice example of populating a TreeTable with business objects and id, parentId, index properties?
Those 3 properties are best practice for storing tree structures in general, so there must be a way in vaadin, too?

As a dirty workaround I’m currently iterating through the TreeTable(!!) and reconstruct my List manually. That can’t be the best practice solution :slight_smile:

thanks!!