I would like to add/remove without the statements being executed until I either create or update A. Is this possible?
There is no way to do this currently. The lazy-loaded collection doesn't even store items in itself so the idea of caching changes for later is not possible.
You could do this on your own however. You could keep a list of items in the entity and then override the dao.create(...)
and dao.update(...)
methods to call collection.addAll(...)
if there are any cached items in your special collection.
private List<Item> foreignItemsToAdd;
...
private void addForeignItem(Item item) {
if (foreignItemsToAdd == null) {
foreignItemsToAdd = new ArrayList<Item>();
}
}
Then the dao would have something like:
@Override
public int create(ParentItem parent) throws SQLException {
if (parent.getForeignItemsToAdd() != null) {
parent.getForeignItems().addAll(parent.getForeignItemsToAdd());
}
return super.create(parent);
}