public static boolean isConstraintViolation ( Exception e ) { Throwable cause = e ; while ( cause != cause . getCause ( ) && cause . getCause ( ) != null ) { if ( cause . getClass ( ) . getSimpleName ( ) . equals ( "ConstraintViolationException" ) ) //$NON-NLS-1$ return true ; cause = cause . getCause ( ) ; } return false ; }
Returns true if the given exception is a unique constraint violation . This is useful to detect whether someone is trying to persist an entity that already exists . It allows us to simply assume that persisting a new entity will work without first querying the DB for the existence of that entity .