Removed entity while in transaction, entity was soft-deleted but can be found when querying while in the same transaction or other

Hi guys,

In my project I needed to use EntityManager instead of DataManager. Because EntityManager loads by default all local attributes the performance was not satisfying so I used loadPartialEntities attribute from View’s.

Please check UtilsService from this sample project (94.9 KB).

You can reproduce it as follows: login with user: admin pwd: admin -> Application -> Screen -> press on test button.

Thank you in advance,
Andrei

Hi Andrei
The issue is caused by the query flush mode which is set automatically to FlushModeType.COMMIT if the view has loadPartialEntities=true. It means that when you load the deleted entity by such query in the same transaction, it reloads the entity in the state how it is now in the database, and effectively loses your in-memory changes (deletion mark in your case). See about query flush mode here.

To fix the issue, just add entityManager.flush(); right after entityManager.remove(shop_client); to write changes to the database before executing the query.

Also, take a look at TransactionalDataManager - it allows you to control transactions and at the same time supports partial detached entities. I strongly don’t recommend using EntityManager with partial entities in managed state.