Test project demonstrates an error which was added during optimization
Methods get and getIfExists in ApplicationStateManager work different for JPA-Entities: first works correct, second gives ClassCastException.
By invoking ApplicationStateManager.set method EntityApplicationStatePersistenceStrategy.set is used, which transforms instanceof Entity-class to instance of PersistedEntity Class.
By Invoking ApplicationStateManager.get method EntityApplicationStatePersistenceStrategy.get is used, which transforms instanceof PersistedEntity Class back to Entity-class. Therefore this code works correct.
By Invoking ApplicationStateManager.getIfExists method SessionApplicationStatePersistenceStrategy.getIfExists is used (SessionApplicationStatePersistenceStrategy is a superclass for EntityApplicationStatePersistenceStrategy), which casts instanceof PersistedEntity Class to Entity-class. This is incorrect and gives ClassCastException in this code.
In other words, get makes back transformation, getIfExists - not. This is the root of problem.