Tree + POJO + JPA

Hello all

I’m trying to create a tree from database, create a form to edit some fields, and save back the pojo to database.

I managed to create the tree, however, i have problems with the form, and the tree update after the form is commited.

If i load the data into the tree with


......
Tree categories = new Tree();
categories.setImmediate(true);
categories.addListener(new ValueChangeListener() {

       @Override
       public void valueChange(ValueChangeEvent event) {
                   showNotification(event);
       }
  });

 List<Category> lst = categoryManager.getCategories();
 for (int i = 0; i < lst.size(); i++) {
                Category c = lst.get(i);
                categories.addItem(c);
                categories.setChildrenAllowed(c, false);
                categories.setParent(c, category);

                loadChildCategories(c);
 }

.......

then, if i create a form, and use the tree item to populate it, i get an empty form


 public void showNotification(ValueChangeEvent event) {
       
        Form form = new Form();
        form.setCaption("Category Editor");
        form.setItemDataSource(categories.getItem(categories.getValue()));

        .........
        Button okbutton = new Button("Save", form, "commit");
        okbutton.addListener(new Button.ClickListener() {       
            @Override
            public void buttonClick(Button.ClickEvent event) {
               categories.commit();

               Item itm = categories.getItem(categories.getValue());

                for (Iterator it = itm.getItemPropertyIds().iterator(); it.hasNext();) {
                    Object object = it.next();
                    System.out.println("IDs "+object);
                }
            }
        });

        okbar.addComponent(okbutton);
        form.getFooter().addComponent(okbar);
.......

Also, i get no ids when the save button is pressed.

However, if i add to the tree new BeanItem(category) , i get the form, but, then on commit, the tree is not updated with the changed values.

I’m missing something?

From the JavaDocs for form.setItemDataSource(Item item):

A Tree has a container in it’s background which holds all the items you add. A Container is a set of Items, and an Item is a set of Properties. You have probably worked with items and their properties if you have used Table. If not, check in Book of Vaadin about
Containers
and about
Tables
to see an example of this in use.

The issue your having is that your container (and thus your items too) has no properties. This is okay for the Tree as it will just use itemId.toString() to display a name for every item, but this wouldn’t work for a Table. As stated in the JavaDocs, the form also requires these properties to know which info to show and what not to show.

A BeanItemContainer would probably have been ideal for you as it is a IndexedContainer that automatically sets up all the properties according to the objects getter/setter pairs. Only problem here is that you are using a Tree which needs a hierarchical container and BeanItemContainer doesn’t implement Container.Hierarchical

tree.setContainerDataSource(new BeanItemContainer<Category>(categoryManager.getCategories()));

ContainerHierarchicalWrapper allows you to add hierarchy support on top of another container, such as BeanItemContainer, but you will need to set the parent-child relationships yourself when using it.

Tanks for the answers, i managed to make it work last night, with BeanItem :slight_smile:

hello can you send me i sample code how you did

thansk