JPA Exceptions
JPA exceptions are represented by a hierarchy of unchecked exceptions:
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
javax.persistence.PersistenceException
javax.persistence.EntityExistsException
javax.persistence.EntityNotFoundException
javax.persistence.LockTimeoutException
javax.persistence.NonUniqueResultException
javax.persistence.NoResultException
javax.persistence.OptimisticLockException
javax.persistence.PessimisticLockException
javax.persistence.QueryTimeoutException
javax.persistence.RollbackException
javax.persistence.TransactionRequiredException
Exception Class Overview
The root of the JPA exception class subtree is:
General errors are represented directly by the PersistenceException
class. Some specific errors are represented by subclasses of PersistenceException
. For example, an attempt to update the database with no active transaction is represented by:
Database update failures that require transaction rollback are represented by:
The RollbackException
exception usually wraps the real failure cause as a nested exception (which can be obtained by the getCause
method) . For example, if a transaction fails because of an attempt to store a new entity object with a primary key that is already in use by another existing entity object, the nested exception is:
Other common causes for RollbackException
are lock failures:
- javax.persistence.LockTimeoutException - JPA Exception
- javax.persistence.OptimisticLockException - JPA Exception
- javax.persistence.PessimisticLockException - JPA Exception
Exceptions can also be thrown on an attempt to retrieve entity objects from the database. For example, when a required entity object is not found:
or during query execution:
- javax.persistence.NonUniqueResultException - JPA Exception
- javax.persistence.NoResultException - JPA Exception
- javax.persistence.QueryTimeoutException - JPA Exception
The lock exceptions (above) may also be thrown during retrieval, if database lock has been requested for retrieved entity objects and it cannot be granted.