Deep Composition still violates Non-Null Exception

I have been following the guide in Documentation regarding deep composition. And I find this is very useful idea.
Now, here’s the scenario:

I have Entity A, that has lists of Entity B.
Entity A has property of amount.

In Web UI Screen, I use form and datasource. All according to the documentation. But, I want to change that everytime user edit “amount” in Entity A, it’ll automatically add new Entity B in the list.

So here’s the code example:


EntityB b = metadata.create(EntityB.class);
b.setTheA(getItem()); //Which refers to the current datasource of EntityA
bDs.addItem(b); //bDs is nested Collection datasource of current EntityA
But everytime I saved the data, it always throws error of violates non-null restriction. So, how can I add the new item in the datasource?

Hi,

It works for me:


public class EntityAEdit extends AbstractEditor<EntityA> {

    @Inject
    private Metadata metadata;
    @Inject
    private Datasource<EntityA> entityADs;
    @Inject
    private CollectionDatasource<EntityB, UUID> listOfBDs;

    @Override
    protected void postInit() {
        entityADs.addItemPropertyChangeListener(e -> {
            if ("name".equals(e.getProperty())) {
                EntityB b = metadata.create(EntityB.class);
                b.setName("A name is now " + e.getValue());
                b.setEntityA(getItem()); //Which refers to the current datasource of EntityA
                listOfBDs.addItem(b); //bDs is nested Collection datasource of current EntityA
            }
        });
    }
}

The whole project is attached.

comp.zip (28.6K)