Source:
<code>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; } <START> } <END> } 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; }</code><technical_language>if of external IDs match? Do give a error message? do fall super-generic "Accounts table constraint prevented save update"? Do a unit test for this case</technical_language>

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; }