Search code examples
hibernatejava-ee-6

What exception to throw when a user tries to edit a deleted entity


In a Java EE web app (JSF) I have an EJB that handles "edition" of an entity.

What exception do I have to throw in the case of this scenario:

  • User 1 enters in the page edition.
  • User 2 deletes the entity from the database.
  • User 1 clicks a "Save" edition.
  • What exception to throw to inform the user that what he was editing has been deleted?

I don't know what exception to throw. The EJB method will also be called remotely. This is why I am looking for an already implemented exception.


Solution

  • Perhaps use an IllegalStateException:

    Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

    Also poissible is ConcurrentModificationException:

    This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.