How to handle JPA unique constraint violations?

后端 未结 8 1784
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 14:05

When a unique constraint is violated, a javax.persistence.RollbackException is thrown. But there could be multiple reasons to throw a RollbackException

8条回答
  •  北海茫月
    2020-12-05 14:17

    compiler returns the exception SQLIntegrityConstraintViolationException, while trying to violate unique constraint.

    Use the following catch block concept, to handle proper exceptions.

    catch(SQLIntegrityConstraintViolationException e) 
    {
      // Error message for integrity constraint violation
    }
    catch(Exception e)
    {
     // Other error messages
    }
    

提交回复
热议问题