jmix
jmix copied to clipboard
Entity cache is not invalidated after entity soft-deleted
Environment
Jmix version: 2.7.1
Bug Description
dataManager.remove(..); does not trigger entity cache invalidation. Loading by id (dataManager.load(..).id(..).one()) returns soft deleted entity when cache enabled and throws NoResultException for the same situation when chache is disabled for entity
Steps To Reproduce
- Open project jmixCaheTest27.zip
- Run test
com.company.jmixcahetest27.Ent1Test#test_saveAndLoadwith and without lineeclipselink.cache.shared.Ent1=truein application.properties E.R.: Results are the same (NoResultException) A.R.: Entity is loaded if cahce is enabled.
Proposed solution
Invalidate entity cache in io.jmix.eclipselink.impl.JmixEntityManager#remove
(entityManager.getEntityManagerFactory().getCache().evict(..))
WA
Clear entity cache manually after soft delete:
@Component
public class EntityCacheClearingEventListener {
@PersistenceContext
EntityManager em;
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void onChangedBeforeCommit(final EntityChangedEvent<?> event) {
if (event.getType() == EntityChangedEvent.Type.DELETED) {
em.getEntityManagerFactory().getCache().evict(event.getEntityId().getEntityClass(), event.getEntityId().getValue());
}
}
}
Source: forum topic.