Source:
public RuntimeException convert(PersistenceException exception, Object entity) { if (exception instanceof OptimisticLockException) { return new ConcurrentModificationException( "Account has the wrong version number; it may have been saved in the background."); } if (exception.getCause() instanceof org.hibernate.exception.ConstraintViolationException) { Throwable cause = Throwables.getRootCause(exception); String message = cause.getMessage(); if (message != null && entity != null) { HibernateAccount account = (HibernateAccount)entity; EntityAlreadyExistsException eae = null; if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-ExternalId-Index'")) { account = (HibernateAccount)BridgeUtils.filterForSubstudy(account); for (String externalId : BridgeUtils.collectExternalIds(account)) { eae = createEntityAlreadyExistsException("External ID", AccountId.forExternalId(account.getStudyId(), externalId)); if (eae != null) { break; } } } else if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-Email-Index'")) { eae = createEntityAlreadyExistsException("Email address", AccountId.forEmail(account.getStudyId(), account.getEmail())); } else if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-Phone-Index'")) { eae = createEntityAlreadyExistsException("Phone number", AccountId.forPhone(account.getStudyId(), account.getPhone())); } if (eae != null) { return eae; } } return new ConstraintViolationException.Builder() .withMessage("Accounts table constraint prevented save or update.").build(); } return exception; }

Target:
public RuntimeException convert(PersistenceException exception, Object entity) { if (exception instanceof OptimisticLockException) { return new ConcurrentModificationException( "Account has the wrong version number; it may have been saved in the background."); } if (exception instanceof NonUniqueObjectException) { return new ConstraintViolationException.Builder().withMessage(NON_UNIQUE_MSG).build(); } if (exception.getCause() instanceof org.hibernate.exception.ConstraintViolationException) { Throwable cause = Throwables.getRootCause(exception); String message = cause.getMessage(); if (message != null && entity != null) { HibernateAccount account = (HibernateAccount)entity; EntityAlreadyExistsException eae = null; if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-ExternalId-Index'")) { for (String externalId : BridgeUtils.collectExternalIds(account)) { eae = createEntityAlreadyExistsException("External ID", AccountId.forExternalId(account.getStudyId(), externalId)); if (eae != null) { break; } } } else if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-Email-Index'")) { eae = createEntityAlreadyExistsException("Email address", AccountId.forEmail(account.getStudyId(), account.getEmail())); } else if (message.matches("Duplicate entry.*for key 'Accounts-StudyId-Phone-Index'")) { eae = createEntityAlreadyExistsException("Phone number", AccountId.forPhone(account.getStudyId(), account.getPhone())); } if (eae != null) { return eae; } } return new ConstraintViolationException.Builder() .withMessage("Accounts table constraint prevented save or update.").build(); } return exception; }