Version 4.10.0

hirondelle.web4j.database
Class DuplicateException

Object
  extended by Throwable
      extended by Exception
          extended by hirondelle.web4j.model.AppException
              extended by hirondelle.web4j.database.DAOException
                  extended by hirondelle.web4j.database.DuplicateException
All Implemented Interfaces:
MessageList, Serializable

public final class DuplicateException
extends DAOException

Thrown when a uniqueness problem occurs in the datastore during an ADD or CHANGE operation.

This type of exception is singled out since it is so common. It allows Actions to catch this specific kind of exception.

For relational databases, this exception should be thrown for INSERT and UPDATE operations which may violate a UNIQUE or PRIMARY KEY constraint, or similar item. Db, DbTx, and TxTemplate will throw a DuplicateException exception for SQLExceptions having an error code matching the ErrorCodeForDuplicateKey configured in web.xml. See web.xml for more information.

Typical Use Case

Here, an Action is calling a DAO method which may throw a DuplicateException:
private void addSomething throws DAOException {
  //this try..catch is needed only if the operation 
  //can have a duplicate problem
  try {
    dao.addSomething();
  }
  catch (DuplicateException ex){
    addError("Cannot add. That item already exists.");
  }
}

Note that if the operation cannot have a duplicate problem, then the Action should not attempt to catch DuplicateException.

Here is the DAO operation which may have a duplicate problem.

//It is highly recommended, but optional, to declare 
//DuplicateException in this method header, to bring 
//it to the attention of the caller
public addSomething() throws DAOException, DuplicateException {
  //...elided
}
Again, if the operation cannot have a duplicate problem, then the DAO should not declare a DuplicateException in its throws clause.

The Db.add(SqlId, Object[]) and Db.edit(SqlId, Object[]) methods can throw a DuplicateException.

See Also:
Serialized Form

Constructor Summary
DuplicateException(String aMessage, Throwable aRootCause)
          Constructor.
 
Method Summary
 
Methods inherited from class hirondelle.web4j.model.AppException
add, add, add, getMessages, isEmpty, isNotEmpty, toString
 
Methods inherited from class Throwable
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DuplicateException

public DuplicateException(String aMessage,
                          Throwable aRootCause)
Constructor.

Arguments are passed to DAOException.DAOException(String, Throwable).


Version 4.10.0

Copyright Hirondelle Systems. Published October 19, 2013 - User Guide - All Docs.