Remove entity not committed - FieldGroup - JPAContainer

Hi,

I am having a hard time getting FieldGroup to work probably.

I have an entity called ePart wich contains a List. I have written a class named AddressTable which extends CustomField which I bound to the FieldGroup to display those addresses. When trying to remove an item from the table it is not commited to the database.

Can someone please show me how to do it? Its hard for me to figure out it myself. There is not much about FieldGroup in the book. Stuff I have found is mostly for the deprecatded Form class. Examples I have read have not been very helpful either.

public class EPartForm extends CustomComponent {
    
    private Item item;
    
    private FormLayout formLayout;
    private FieldGroup fieldGroup;
    
    private TextField id;
    private TextField model;
    
    private Button buCommit;
    private Button buDiscard;
    private Button buRemoveManufacturer;
    
    public EPartForm(Item item) {
        this.item = item;
        create();
    }
    
    private void create() {
        formLayout = new FormLayout();
        
        id = new TextField("ID");
        formLayout.addComponent(id);
        
        model = new TextField("Model");
        model.addValidator(new BeanValidator(EPart.class, "model"));
        formLayout.addComponent(model);
        
        AddressTable addresses = new AddressTable();
        addresses.setPropertyDataSource(item.getItemProperty("addresses"));
        formLayout.addComponent(addresses);        
        
        fieldGroup = new FieldGroup(item);
        
        fieldGroup.setFieldFactory(new DefaultFieldGroupFieldFactory() {
            @Override
            public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
                if(type.isAssignableFrom(List.class)) {
                    return (T) new AddressTable();
                }
                return super.createField(type, fieldType);
            }            
        });
        
        fieldGroup.bindMemberFields(this);
        
        FormClickListener clickListener = new FormClickListener();
        
        buCommit = new Button("Commit", clickListener);
        formLayout.addComponent(buCommit);
        
        buDiscard = new Button("Discard", clickListener);
        formLayout.addComponent(buDiscard);
        
        buRemoveManufacturer = new Button("Remove manufacturer", clickListener);
        formLayout.addComponent(buRemoveManufacturer);

        setCompositionRoot(formLayout);
    }
    
    public void setItemDataSource(Item item) {
        fieldGroup.setItemDataSource(item);
    }
    
    class AddressTable extends CustomField {

        Table adrTable = new Table();
        BeanItemContainer<EPartAddress> container = new BeanItemContainer<EPartAddress>(EPartAddress.class);
        
        Button buRemove;
        
        public AddressTable() {
            create();
        }
        
        private void create() {
            adrTable.setContainerDataSource(container);
            adrTable.setSelectable(true);
            
            buRemove = new Button("Remove selected");
            buRemove.addClickListener(new ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent event) {
                    container.removeItem(adrTable.getValue());
                    try {
                        fieldGroup.commit();
                    } catch(FieldGroup.CommitException ex) {
                        Logger.getLogger(EPartForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }

        @Override
        protected Component initContent() {
            VerticalLayout layout = new VerticalLayout();
            layout.addComponent(adrTable);
            layout.addComponent(buRemove);
            return layout;
        }
        
        @Override
        public void setPropertyDataSource(Property newDataSource) {
            Object value = newDataSource.getValue();
            if(value instanceof List<?>) {
                List<EPartAddress> beans = (List<EPartAddress>)value;
                container.removeAllItems();
                container.addAll(beans);
                adrTable.setPageLength(beans.size());
            } else {
                throw new ConversionException("Invalid type");
            }
        }
        
        @Override
        public Object getValue() {
            ArrayList<EPartAddress> beans = new ArrayList<>();
            for (Object itemId: container.getItemIds())
                beans.add(container.getItem(itemId).getBean());
            return beans;
        }        
        
        @Override
        public Class getType() {
            return ArrayList.class;
        }

        
    }
    
    class FormClickListener implements ClickListener {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            if(event.getButton().equals(buCommit)) {
                try {
                    fieldGroup.commit();
                } catch(FieldGroup.CommitException e) {
                    Notification.show("Constraints violation:", e.getCause().getLocalizedMessage(), Type.ERROR_MESSAGE);
                }
            } else if(event.getButton().equals(buDiscard)) {
                fieldGroup.discard();
                Notification.show("Discarded!", Type.TRAY_NOTIFICATION);
            } else if(event.getButton().equals(buRemoveManufacturer)) {
                try {
                    item.getItemProperty("manufacturer").setValue(null);
                    fieldGroup.commit();
                } catch(FieldGroup.CommitException ex) {
                    Logger.getLogger(EPartForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}