Is there a more elegant way of avoiding javax.persistence.EntityNotFoundException
when calling EntityManager.remove()
on an object that may or may not currently exist in persistent state? I am trying to avoid a situation where I need to make 2 queries to remove an object. Currently I am cheating with:
void remove(String id) {
T model = entityManager.getReference(type, id);
entityManager.remove(model);
}
But this will throw an exception if the model does not exist.
I could:
void remove(String id) {
T model = retrieve(id);
if(model != null)
entityManager.remove(model);
}
But that would involve 2 queries (setting aside the notion of a cache for now).