Hello together,
I have two Entitys with a “MANY_TO_MANY” relationship and I want to Add and Remove Items to one of the entitys, via “ticking” a Checkbox (generated Columnd of a Table):
The Table has a CollectionDatasource of the “opposite Entity” which contains all of the Items the user might want to add (or remove).
Please have a look at the following code of mine:
@Override
protected void postInit() {
super.postInit();
listeGebiete = auftragnehmerDs.getItem().getAnPlzGebietFK();
}
public Component generateGebietZugewiesenCell(GlobalPlzGebiet entity) {
CheckBox cbox = (CheckBox) componentsFactory.createComponent(CheckBox.NAME);
if(listeGebiete != null) {
for (GlobalPlzGebiet gebiet : listeGebiete) {
if(gebiet.getId().equals(entity.getId())) {cbox.setValue(true); break;}
}
}
cbox.addValueChangeListener(evt -> changePlzGebiet(evt));
return cbox;
}
private void changePlzGebiet(ValueChangeEvent evt) {
if ((boolean) evt.getValue()) {
auftragnehmerDs.getItem().getAnPlzGebietFK().add(globalPlzGebietsDs.getItem());
//auftragnehmerDs.commit();
}
else {
auftragnehmerDs.getItem().getAnPlzGebietFK().remove(globalPlzGebietsDs.getItem());
//auftragnehmerDs.commit();
}
}
This works “kinda fine”, as long as i call the Datasource commit after adding or removing the Item. But i assume thats not the way, it was designed… for example there is no longer any need for the “ok” , “Cancel” Button of the Edit Screen.
Without calling the commit, no changes get saved to the Database. Also when pressing Cancel, There is no message “you have unsaved changes”, which usually appears when i change something.
Can you please tell me what am i doing wrong ? i tried calling isModified but that doens’t change anything.
Thanks and best Regards
Daniel