For modifying queries you need to add an @Modifying
to the method.
Be sure you are aware of the side effects of the approach you chose:
- Executing a manipulating query is pretty much bypassing all
EntityManager
caches. Thus a subsequentfindOne(…)
might/will still return the old instance of the object you tried to delete in case theEntityManager
had already loaded it. To prevent that, set theclearAutomatically
flag in@Modifying
totrue
but be aware that this will cause all pending changes being wiped out. - For query based data manipulation no lifecycle callbacks will be triggered and no cascades will be triggered on the level of the persistence context. This means, entity listeners listening to an
@PreUpdate
event will not get notified. Also any cascade operations