dataManager not really removing Entity

I have this scenario where I need to reorganise items in a CollectionContainer.

Suppose each SalesOrder has many items, with type: SalesOrderItem.

Now, in the code, I try to do the following:

Map<SaleItem, SalesOrderItem> orderItems = orderItemsDc.getItems()
                .stream()
                .filter(o -> o.getCancelledBy() == null &&
                        o.getComplimentaryBy() == null &&
                        !o.getItem().getCategory().getName().equals("Billiard"))
                .collect(Collectors.toMap(SalesOrderItem::getItem, item -> {
                    item.setPrinted(true);
                    return item;
                }, (item1, item2) -> {
                    item1.setQty(item1.getQty() + item2.getQty());
                    item1.setTotalPrice(item1.getTotalPrice() + item2.getTotalPrice());
                    item1.setPrinted(true);

                    dataManager.save(item1);
                    dataManager.remove(item2);

                    return item1;
                }));

The problem, if I simply do that, the dataContext will think that the tracked entity still dirty. And if I use dataContext.save then it will results in data duplicates.

Any idea on how we can do this? More specifically, I wonder why dataManager doesn’t really remove the Entity and that it doesn’t work together with the dataContext.

@yp1122

Hello Yosi

I believe that you should use dataContext.evict(item2) after dataManager.remove(item2).

Good luck and best regards
Chris

1 Like

Or maybe DataContext.remove(item2)?
Quote from the Javadocs:

Removes the entity from the context and registers it as deleted. The entity will be removed from the data store upon subsequent call to save().

Thank you for the help. My problem was solved when I use dataContext.remove(), and in the collect, instead of just returning item1, I should have returned the dataManager.save(item1).

So my question remains, why dataManager doesn’t work? What’s the guideline here, when to use dataContext or dataManager?

DataManager is a universal tool for CRUD operations. It can be used in Spring beans, views, tests, etc. It is completely stateless - you pass an entity or query to it and it immediately returns the result - a loaded or updated entity.

DataContext is designed to work in views. It is a buffer that contains all entities loaded into the view. It automatically tracks what has been changed and saves the changed entities when user clicks “OK” and DataContext.save() method is called. The DataContext.save() invokes DataManager.save() in turn if you haven’t provided your save delegate.

When working with entities in a view, you should keep in mind that they are normally in the DataContext buffer. So you should use the DataContext API to save or remove them.

DataContext is not mandatory, you could manage entities in a view manually. To avoid DataContext completely, specify <data readOnly="true"> in the view XML. Or you could define <loader readOnly="true"> to bypass DataContext for entities loaded by this loader.

2 Likes