Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice propagating Unique Violation Exceptions to UI
    primarykey
    data
    text
    <p>We are working on Java web project based on JPA 2, Hibernate, Spring 3 and JSF 2 running in Tomcat 7. We are using Oracle 11g as database. </p> <p>We are currently holding a debate on approaches to populate database constraint violations as user-friendly messages to the UI. More or less we see two ways, both are not really satisfying. Could somebody give some advise?</p> <p><strong>Approach 1 - Validate programmatically and throw specific exception</strong></p> <p>In CountryService.java each Unique constraint will be validated and a corresponding exception is thrown. The exceptions are handled individually in a backing bean.</p> <p><em>Advantage</em>: Easy to understand and maintain. Specific User messages possible.</p> <p><em>Disadvantage</em>: A lot of code just for having nice messages. Basically all DB Constraints are written again in the application. A lot of queries - unnecessary db load.</p> <pre class="lang-java prettyprint-override"><code>@Service("countryService") public class CountryServiceImpl implements CountryService { @Inject private CountryRepository countryRepository; @Override public Country saveCountry(Country country) throws NameUniqueViolationException, IsoCodeUniqueViolationException, UrlUniqueViolationException { if (!isUniqueNameInDatabase(country)) { throw new NameUniqueViolationException(); } if (!isUniqueUrl(country)) { throw new UrlUniqueViolationException(); } if (!isUniqueIsoCodeInDatabase(country)) { throw new IsoCodeUniqueViolationException(); } return countryRepository.save(country); } } </code></pre> <p>In the View's Backing Bean you handle the exceptions:</p> <pre class="lang-java prettyprint-override"><code>@Component @Scope(value = "view") public class CountryBean { private Country country; @Inject private CountryService countryService; public void saveCountryAction() { try { countryService.saveCountry(country); } catch (NameUniqueViolationException e) { FacesContext.getCurrentInstance().addMessage("name", new FacesMessage("A country with the same name already exists.")); } catch (IsoCodeUniqueViolationException e) { FacesContext.getCurrentInstance().addMessage("isocode", new FacesMessage("A country with the same isocode already exists.")); } catch (UrlUniqueViolationException e) { FacesContext.getCurrentInstance().addMessage("url", new FacesMessage("A country with the same url already exists.")); } catch (DataIntegrityViolationException e) { // update: in case of concurrent modfications. should not happen often FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The country could not be saved.")); } } } </code></pre> <p><strong>Approach 2 - Let the database detect constraint violations</strong></p> <p><em>Advantage</em>: No boiler plate code. No unnecessary queries to db. No duplication of data constraint logic.</p> <p><em>Disadvantage</em>: Dependencies to constraint names in DB, so generation of Schema through hibernate not possible. Mechanism needed to bind messages to input components (e.g. for highlighting).</p> <pre class="lang-java prettyprint-override"><code>public class DataIntegrityViolationExceptionsAdvice { public void afterThrowing(DataIntegrityViolationException ex) throws DataIntegrityViolationException { // extract the affected database constraint name: String constraintName = null; if ((ex.getCause() != null) &amp;&amp; (ex.getCause() instanceof ConstraintViolationException)) { constraintName = ((ConstraintViolationException) ex.getCause()).getConstraintName(); } // create a detailed message from the constraint name if possible String message = ConstraintMsgKeyMappingResolver.map(constraintName); if (message != null) { throw new DetailedConstraintViolationException(message, ex); } throw ex; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload